How to disable MySQL foreign key checks and what are the benefits of disabling it?

MySQLMySQLi Database

We can disable foreign key checks with the help of the following statement −

mysql> Set foreign_key_checks = 0;
Query OK, 0 rows affected (0.00 sec)

And we can enable it with the help of the following statement −

mysql> Set foreign_key_checks = 1;
Query OK, 0 rows affected (0.00 sec)

Some benefits of disabling foreign key checks are as follows −

  • After disabling foreign key checks we can load data into parent and child table in any order. Otherwise, we must have to load the data first in the parent table and then in the child table.
  • Without disabling foreign key checks we cannot drop a table that is referenced by a foreign key constraint.
raja
Published on 19-Feb-2018 10:23:39
Advertisements