I have two tables that are linked by a foreign key constraint. I would like to change an ID field but the following code fails due to the foreign key constraint:
UPDATE A SET id = 1479 WHERE id = 2103;
UPDATE B SET Aid = 1479 WHERE Aid = 2103;
I know I can set up ON CASCADE to do this automatically, but how can I do it on a case-by-case basis like this?
By disabling foreign key checks and by running everything within a transaction.
START TRANSACTION;
SET FOREIGN_KEY_CHECKS = 0;
UPDATE A SET id = 1479 WHERE id = 2103;
UPDATE B SET Aid = 1479 WHERE Aid = 2103;
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With