How to disable foreign key checks – MySQL

From time to time you’ll need to delete something from your database and when you try to do it, the MySQL will complain on foreign keys (beside the fact that other table is empty)

This can be solved with disabled foreign keys

SET foreign_key_checks = 0;
DELETE FROM users WHERE id > 45;
SET foreign_key_checks = 1;

You probably noticed that after I deleted those records, I enabled foreign key checks again which you should do also.

Leave a Reply

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