{"id":7241,"date":"2012-09-04T16:52:58","date_gmt":"2012-09-04T11:22:58","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7241"},"modified":"2012-09-23T18:00:44","modified_gmt":"2012-09-23T12:30:44","slug":"toggling-foreign-key-checks-in-mysql","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/toggling-foreign-key-checks-in-mysql\/","title":{"rendered":"Toggling foreign key checks in MySQL"},"content":{"rendered":"<p>Recently in my project, we needed to import database dumps of a legacy database in MySQL, which was involved in a nested relationship where one tuple could be the parent record of another tuple. We created the appropriate domain structure and checked that the foreign key references were created accordingly. All well and good.<br \/>\n<br \/>\nSo we started to restore the dump for the database. But soon problems started arising when certain records had references to records that were not yet created i.e, that is some records were being assigned parents that did not exist. We needed a way to turn of foreign key checking for some time. So after taking a look at the MySQL documentation (<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/innodb-foreign-key-constraints.html\">http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/innodb-foreign-key-constraints.html<\/a>), I stumbled across a way to turn off foreign key checking by running the following command:<br \/>\n<br \/>\n[code]mysql&gt; SET FOREIGN_KEY_CHECKS = 0<br \/>\nmysql&gt; SOURCE fakeDB<br \/>\nmysql&gt; SET FOREIGN_KEY_CHECKS = 1[\/code]<br \/>\n<br \/>\nSo there it was. We had happily imported our legacy database with all the foreign key checks intact.<br \/>\n<br \/>\nIf you&#8217;re not sure about the status of foreign key checking in your MySQL instance, you can use the following command to check the status:<br \/>\n[code]mysql&gt; select * from INFORMATION_SCHEMA.GLOBAL_VARIABLES where VARIABLE_NAME like &#8216;FOREIGN_KEY_CHECKS&#8217;;[\/code]<br \/>\n<br \/>\nHope this helps.<br \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently in my project, we needed to import database dumps of a legacy database in MySQL, which was involved in a nested relationship where one tuple could be the parent record of another tuple. We created the appropriate domain structure and checked that the foreign key references were created accordingly. All well and good. So [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[943,76,944],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7241"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7241"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7241\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}