In MySQL, can I defer referential integrity checks until commit
Asked Answered
B

3

68

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.

Botanize answered 16/2, 2011 at 9:32 Comment(0)
B
86

Looks like my answer is here...

Like MySQL in general, in an SQL statement that inserts, deletes, or updates many rows, InnoDB checks UNIQUE and FOREIGN KEY constraints row-by-row. When performing foreign key checks, InnoDB sets shared row-level locks on child or parent records it has to look at. InnoDB checks foreign key constraints immediately; the check is not deferred to transaction commit. According to the SQL standard, the default behavior should be deferred checking. That is, constraints are only checked after the entire SQL statement has been processed. Until InnoDB implements deferred constraint checking, some things will be impossible, such as deleting a record that refers to itself using a foreign key.

Back to the drawing board.

Botanize answered 16/2, 2011 at 9:37 Comment(7)
@rossmcf +1 as I have been searching the documentation for this sentence :DOlwena
If this is an important feature for you, you can consider migrating to PostgreSQL which supports deferred constraints (including deferred unique constraints since 9.0) - and you'd get a lot of other nifty features as well (recursive queries, windowing functions, check constraints, ...)Diversity
The feature request for this has been around since 2004: bugs.mysql.com/bug.php?id=7529 Any day now...Shemeka
The language appears to have moved to point 3 hereAlgesia
@Algesia if I'm not mistaken, that's literally what he quoted in his answerGeyer
@BrianLeishman Yes, Alexander Azarov was kind enough to edit the post.Algesia
@Algesia I see that now, I should have noticed the edit being so recent!Geyer
D
20

If you are asking if MySQL supports the DEFERRABLE attribute for foreign keys (including the option INITIALLY 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".

Diversity answered 16/2, 2011 at 10:59 Comment(2)
I still can't find anything related to this. Is the answer still a "NO" ? I am thinking of switching to postgresql for all the good reasons.Ethanol
@akki: yes, the answer is still NO. MySQL does not support deferrable constraints (or any other modern SQL features)Diversity
K
14

You may handle this limitation of innodb engine, by temporarily disabling foreign key checks by setting server variable:

set foreign_key_checks=0;

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:

mysql> SET foreign_key_checks = 0;
mysql> SOURCE dump_file_name;
mysql> SET foreign_key_checks = 1;
Kielty answered 30/4, 2012 at 14:14 Comment(3)
I'm not sure that this would still check the constraints after the update, though, which is what I was looking for.Botanize
No, it does not and therefore this the option foreign_key_checks=0 and similar options like disabled the uniqueness check are very, very, very dangerous. If it was so easy, then MySQL would probably also have implemented deferred constraints. These options are only meant to speed up backup and restore when you know for sure that no constraint will be violated. You must not use this option as a work-around for MySQL's lack of deferred constraint. If you re-enable the option, then no check is repeated. In particular, if the constraint is violated, then MySQL may show undefined behavior.Ascites
This behaviour is particularly problematic if you’re trying to insert multiple rows into a table which references itself (such as a supervisor id in the same table). The set foreign_key_checks statement helps here.Antilogy

© 2022 - 2024 — McMap. All rights reserved.