Home >Database >Mysql Tutorial >Why Does 'Bogus Foreign Key Constraint Fail' Error Occur When Dropping Tables?
"Bogus Foreign Key Constraint Fail" Explained and Resolved
When encountering the error message "ERROR 1217 (23000) at line 40: Cannot delete or update a parent row: a foreign key constraint fails" while attempting to delete a table, it's important to understand the underlying cause. Despite dropping all other tables with foreign key references to the problematic table, the error persists.
In this scenario, the issue arises from MySQL Query Browser or phpMyAdmin using separate connections for each query. To resolve this, all drop statements should be consolidated into a single query as follows:
SET FOREIGN_KEY_CHECKS=0; DROP TABLE my_first_table_to_drop; DROP TABLE my_second_table_to_drop; SET FOREIGN_KEY_CHECKS=1;
By doing so, the foreign key checks are temporarily disabled, allowing for the successful deletion of the tables. The final SET FOREIGN_KEY_CHECKS=1 statement re-enables the foreign key checks, ensuring data integrity.
It's important to note that InnoDB does not support cross-schema foreign keys. Thus, the issue is not caused by any external references outside the current database schema.
The above is the detailed content of Why Does 'Bogus Foreign Key Constraint Fail' Error Occur When Dropping Tables?. For more information, please follow other related articles on the PHP Chinese website!