MySQL Error while dropping tables: Cannot delete or update a parent row: a foreign key constraint fails

If you need to drop several tables, for example because you need to then restore them, you may arrive at the following error:

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

That happens if there are tables with foreign keys references to the table you are trying to drop.

All you need to do is:

SET FOREIGN_KEY_CHECKS=0

Drop your tables

SET FOREIGN_KEY_CHECKS=1

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

5 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.