As in this question, I've been reading PoEAA and wondering if it's possible to defer referential integrity checks until commit in MySQL.
I've run into this problem when wanting to insert a bunch of products and related products in the same commit. Even within a transaction, I get constraint errors when I try to insert into the related_products
join table.
If it helps, I'm using PHP PDO for database connections.
I'd appreciate any help you could offer.
Looks like my answer is here...
Back to the drawing board.
If you are asking if MySQL supports the
DEFERRABLE
attribute for foreign keys (including the optionINITIALLY DEFERRED
) then the answer is a clear no.You can't defer constraint checking until commit time in MySQL.
And - as you have already pointed out - they are always evaluated at "row level" not on "statement level".
You may handle this limitation of innodb engine, by temporarily disabling foreign key checks by setting server variable:
From MySQL manual:
mysqldump also produces correct definitions of tables in the dump file, and does not forget about the foreign keys.
To make it easier to reload dump files for tables that have foreign key relationships, mysqldump automatically includes a statement in the dump output to set foreign_key_checks to 0. This avoids problems with tables having to be reloaded in a particular order when the dump is reloaded. It is also possible to set this variable manually: