问题描述
我将数据插入到TEMP1.aa
列中(temp1中的主键),但不会导入到TEMP2.cc
列中(temp2中的外键).
I insert data in the column TEMP1.aa
(primary key in temp1), but it doesn't import into the TEMP2.cc
column (foreign key in TEMP2).
为什么?
我认为如果在主键中插入数据,它将自动插入到外键中!如果是这样,那么如果我们插入主键,必须更新外键吗?
I thought if insert data in primary key it automatic insert into foreign key! If this true that if we insert in a primary key, foreign key must be updated?
推荐答案
外键不会更新子引用,它只能确保存储在该列中的值已经存在于父表中.
A foreign key does not update child references, it only ensures that the value stored in the column already exists in the parent table.
但是MySQL通过在TEMP1表的外键约束中添加ON UPDATE CASCADE
来支持级联更新.否则,您需要考虑使用触发器来制定更详细的规则/要求.
But MySQL supports cascading an update, by adding the ON UPDATE CASCADE
to the foreign key constraint in the TEMP1 table. Otherwise, you need to consider using a trigger for more elaborate rules/requirements.
参考:
- MySQL - Foreign Key Constraints documentation
- MySQL - Triggers documentation
这篇关于为什么外键不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!