I need to update the primary ID value of a record in table_1 and at the same time update the foreign keys referencing that ID value. In the sql below, i have included ALL the tables referencing table_1, however not all of these tables are referencing that particular record.
I am using the sql below:
UPDATE table_1 T1,
table_2 T2,
table_3 T3,
table_4 T4,
table_5 T5,
table_6 T6,
table_7 T7
SET T1.EMI_ID = 15678,
T2.AKN_EMI_FK = 15678,
T3.EXP_EMI_FK = 15678,
T4.HDR_EMI_FK = 15678,
T5.LSU_EMI_FK = 15678,
T6.MUT_EMI_FK = 15678,
T7.IMG_EMI_FK = 15678
WHERE T1.EMI_ID = 77777718765 AND
T2.AKN_EMI_FK = T1.EMI_ID AND
T3.EXP_EMI_FK = T1.EMI_ID AND
T4.HDR_EMI_FK = T1.EMI_ID AND
T5.LSU_EMI_FK = T1.EMI_ID AND
T6.MUT_EMI_FK = T1.EMI_ID AND
T7.IMG_EMI_FK = T1.EMI_ID;
It does not seem to be working and i believe that could be because in some tables that record id does not exist as foreign key.
Is there a better way of doing this?
Yes, by using a foreign key UPDATE constraint:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
Example: