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

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

SET FOREIGN_KEY_CHECKS=0;

Now the drop table SQL command will work. After you dropped the table, re-enable foreign key check with

SET FOREIGN_KEY_CHECKS=1;

Example

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]>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *