gpt4 book ai didi

mysql - 更改表中主键的值和引用表中的 FK

转载 作者:行者123 更新时间:2023-11-29 02:07:48 26 4
gpt4 key购买 nike

我想更改表的主键,并更改引用它的表的外键。

例如表 employee 有 PK idEmployee = 1表 payroll 有 FK idEmployee = 1

我想将员工更新为 idEmployee = 2 并让表 payroll 自动更改 FK idEmployee。

这在 mysql 中可行吗?

谢谢。

最佳答案

是的 - 你必须先设置 ON UPDATE clause在外键约束中:

CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table.

关于mysql - 更改表中主键的值和引用表中的 FK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2909422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com