Sometimes, it is very useful to disable foreign key checks. For example, you can load data to the parent and child tables in any order with the foreign key constraint check disabled. If you don’t disable foreign key checks, you have to load data into the parent tables first and then the child tables in sequence, which can be tedious.
Another scenario that you want to disable the foreign key check is when you want to drop a table. Unless you disable the foreign key checks, you cannot drop a table referenced by a foreign key constraint.
To disable foreign key checks, you set the foreign_key_checks variable to zero as follows:
SET foreign_key_checks = 0;
To re-enable foreign key constraint check, you set the value of the foreign_key_checks to 1:
SET foreign_key_checks = 1;
Notice that setting foreign_key_checks to 1 does not trigger any validation of the existing table data. In other words, MySQL will not verify the consistency of the data that was added during the foreign key check disabled.
Comentarios