ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
When deleting a table in a MySQL database, I get the following error message
1 2 3 |
MariaDB [thrkhztbpt]> drop table users; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails MariaDB [thrkhztbpt]> |
To fix the error, run
1 |
SET FOREIGN_KEY_CHECKS=0; |
Now the drop table SQL command will work. After you dropped the table, re-enable foreign key check with
1 |
SET FOREIGN_KEY_CHECKS=1; |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
MariaDB [thrkhztbpt]> drop table users; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails MariaDB [thrkhztbpt]> SET FOREIGN_KEY_CHECKS=0; Query OK, 0 rows affected (0.00 sec) MariaDB [thrkhztbpt]> drop table users; Query OK, 0 rows affected (0.00 sec) MariaDB [thrkhztbpt]> MariaDB [thrkhztbpt]> SET FOREIGN_KEY_CHECKS=1; Query OK, 0 rows affected (0.00 sec) MariaDB [thrkhztbpt]> |