简体   繁体   中英

On Update Cascade not changing value of foreign key

I have 2 tables being:

'bin' and 'missedbin' ,missed bin contains a foreign key or the primay key 'binID' from bin. I have set the foreign key to cascade on update & delete.

However, when a value is inserted into bin the foreign key is not updated within the missedbin table and remains null. Have I done something incorrectly?

EDIT: missedbin table:

Missedbin bin table:

箱子

I have 2 insert statements running in asp:

cmd.CommandText = "insert into mydb1.bin values(null,'" + binType + "','" + binColour + "','" + personIDdata + "')";

cmd.CommandText = "insert into mydb1.missedbin values (null, '" + personIDdata + "','" + dateFound + "', null)";

The foreign key does not work that way. You have to provide the correct binID when inserting into missedBin (always). You can use LAST_INSERT_ID(). Only if you later change bin.binID, then the binID in missedBin will change as well

INSERT INTO bin () VALUES () ...
INSERT INTO missedbin (binID, ...) VALUES (LAST_INSERT_ID(), ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM