为什么外键不更新? [英] why foreign key does'nt update?

查看:220
本文介绍了为什么外键不更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列 TEMP1.aa (temp1中的主键)中插入数据,但它不会导入到 TEMP2.cc 列(TEMP2中的外键)。

为什么?

主键中的数据自动插入到外键中!如果这是真的,如果我们插入一个主键,外键必须更新?

解决方案

外键不会更新子引用,它只能确保存储在列中的值已经存在于父表中。

但是MySQL通过在TEMP1的外键约束中添加 ON UPDATE CASCADE 来支持级联更新表。否则,您需要考虑使用触发器来制定更详细的规则/要求。



参考:


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).

Why?

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.

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.

Reference:

这篇关于为什么外键不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆