Disable Foreign Key checks or constraint in Mysql

Referential integrity/relation is one of the best feature for database architecuture. But in certain case we need to clear or disable these checks otherwise we can’t do normal modification within our data because if tow tables are following primary key and foreign key constraint then we must have to follow these relation otherwise we may get database errors.

The easiest way to disable foreign key is below command.

SET foreign_key_checks = 0;

DELETE FROM users where id > 45;

SET foreign_key_checks = 1;

By setting the foreign key check to 0, I was able to update / delete my users table. Once I was done with my operations on the user table, I reset the key check to 1 again and everything is back in place now.

Related posts:

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)