Cannot delete or update a parent row: a foreign key constraint fails


When i tried to drop and recreate tables having foreign keys in mysql database. I got following error

Cannot delete or update a parent row: a foreign key constraint fails


ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

When I encountered this before, I ended up dropping the entire database, recreating it, and then restoring the schema.

It's good for small database.

If you are working with large database, then this is not suitable in all cases.

There is very simple solution of this problme:

Turns out you can temporarily disable foreign key checks:

SET FOREIGN_KEY_CHECKS=0;
(Run above SQL query to disable foreign key check)

Once all database table created or imported

Just be sure to restore them once you’re done messing around:

SET FOREIGN_KEY_CHECKS=1;

That's it.
Read More