MySQL: Disable foreign keys while updating database
Originally found here: http://www.stetsenko.net/2008/10/mysql-how-to-ignore-checking-of-foreign-key-constraints-for-innodb-tables/
Making a note of it here because it always take me a while to find the above article.
You can switch off checking of foreign keys while you truncate/populate the tables in a MySQL database with:
SET FOREIGN_KEY_CHECKS = 0;
and re-enable it with:
SET FOREIGN_KEY_CHECKS = 1;
This is especially useful if, like me, you use an SQL script to pre-populate a MySQL database:
USE `database_name`; SET FOREIGN_KEY_CHECKS = 0; TRUNCATE `table_1`; TRUNCATE `table_2`; TRUNCATE `table_3`; TRUNCATE `table_4`; INSERT INTO `table_1` ...; INSERT INTO `table_2` ...; INSERT INTO `table_3` ...; INSERT INTO `table_4` ...; SET FOREIGN_KEY_CHECKS = 0;
- Richard@Home:


Comments
Thanks for the tip :-)
Thanks for the tip :-)
Re: MySQL: Disable foreign keys while updating database
As well, I would wrap the majority of the SQL inside a transaction. I normally have 2 files (for the trunk of my projects), one structure.sql and one data.sql. At the top of each:
And at the bottom of each:
Post new comment