简体   繁体   English

在更新级联上不更改外键的值

[英]On Update Cascade not changing value of foreign key

I have 2 tables being: 我有2张桌子:

'bin' and 'missedbin' ,missed bin contains a foreign key or the primay key 'binID' from bin. 'bin'和'missedbin',missed bin包含外键或bin中的主键'binID'。 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. 但是,将值插入bin中时,外键不会在missedbin表中更新,并且保持为空。 Have I done something incorrectly? 我做错了什么吗?

EDIT: missedbin table: 编辑:missedbin表:

Missedbin bin table: 箱表:

箱子

I have 2 insert statements running in asp: 我有2在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). 插入missedBin(始终)时,您必须提供正确的binID。 You can use LAST_INSERT_ID(). 您可以使用LAST_INSERT_ID()。 Only if you later change bin.binID, then the binID in missedBin will change as well 仅当您以后更改bin.binID时,missedBin中的binID也会更改

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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