Thursday, July 3, 2014

How to update a table when there is a Foreign Key Constraint

When there is a foriegn key constraint, tables cannot be updated easily in MySQL. A solution for that is disable the foreign key constraint option in the particular MySQL server and do the update. Finally we can enable the foreign key constraint in the particular MySQL server. An important thing to remember is when you disable the foreign key constarint in the server, it affects to each and every table in the MySQL server.  

//Disable the foreign key constraint option 
SET FOREIGN_KEY_CHECKS=0 

//Enable the foreign key constraint option 
SET FOREIGN_KEY_CHECKS=1 

 //Check the state of the foreign key option in the MySQL server 
//0 means Disabled, 1 means Enabled 
SELECT @@FOREIGN_KEY_CHECKS