Skip to content

How to temporarily disable a foreign key constraint in MySQL?

In this post you will learn how to temporarily disable foreign key constraints in MySQL Database. There are some scenarios where you have set foreign keys in a table but you would want to ignore the constraint because of the improper dataset. You can temporarily disable key constraints in MySQL per session or globally. You should not that this is only for testing purposes and not for production level data.

To disable foreign key constraint per session use the following SQL command.

SET FOREIGN_KEY_CHECKS=0;

Or globally for all users using the following SQL command.

SET GLOBAL FOREIGN_KEY_CHECKS=0;

To enable the foreign key constraints again, set the same values to 1.

SET FOREIGN_KEY_CHECKS=1;
SET GLOBAL FOREIGN_KEY_CHECKS=1;

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.