FirstSearch Blue

Find Everything You Need On Your First Search

Article

Ignore Foreign Key Constraints During Truncate or Drop

April 17, 2014 Updated February 19, 2017 1 min read A

mysql

If you are trying to drop/delete or empty/truncate a mysql table and you keep getting an error that you are violating a foreign key constraint, you may want to temporarily ignore that constraint. The code below demonstrates how you can tell mysql to ignore foreign key checks, delete/truncate the tables you need to, then re-enable foreign key checks

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE MY_TABLE;
DROP HIS_TABLE;
SET FOREIGN_KEY_CHECKS = 1;

Conversation

Comments

Historic WordPress comments were imported into D1. New submissions stay in review until approved.

Published comments

Loading comments…