开发者

changing value of a primary key in a table and FK in referenced tables

开发者 https://www.devze.com 2022-12-31 20:40 出处:网络
I wish to change the primary key of a table, and also change the foreign key of the tables that reference it.

I wish to change the primary key of a table, and also change the foreign key of the tables that reference it.

for example table employee has PK idEmployee = 1 table payroll has FK idEmployee = 1

I want to update employee to idEmployee = 2 and let the table payroll chang开发者_如何学JAVAe FK idEmployee automatically.

is this posible in mysql?

thanks.


Yes - you have to first set the ON UPDATE clause in the foreign key constraint:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消