简体   繁体   English

更新一个表中的主键,这是另一表中的外键

[英]Update primary key in one table which is foreign key in another table

I have a table that it's primary key is foreign key in 5 tables.I need to change the value of this primary key field.what's the query?我有一个表,它的主键是 5 个表中的外键。我需要更改这个主键字段的值。查询是什么?

             Table1 >> fields: tbl1ID(pk, bigint),  Desc(nvarchar(50))
             Table2 >> fields: tbl2ID(pk, bigint),tbl1ID(fk, bigint),  Desc(nvarchar(50))
             Table3 >>....
             .
             .
             .

I've set tbl1ID=1 and now i need to change it like this tbl1ID=100001, but i cant because this field is related to another table.我已经设置了 tbl1ID=1,现在我需要像这样更改 tbl1ID=100001,但是我不能,因为这个字段与另一个表相关。

is it possible to change all of them?是否有可能改变所有这些?

You can do this simply by temporarily setting foreign_key_checks=0 in your session.您可以通过在会话中临时设置 foreign_key_checks=0 来完成此操作。

SET FOREIGN_KEY_CHECKS = 0;
UPDATE TABLE_A INNER JOIN TABLE_B ON TABLE_B.PKID = TABLE_A.PKID 
SET TABLE_B.PKID = 'NEW_VALUE', TABLE_A.PKID = 'NEW_VALUE'
WHERE TABLE_A.PKID = 'OLD_VALUE';

Another option is to configure the foreign key with the ON UPDATE CASCADE option so that if the primary key is updated on the parent table it will cascade to the child table另一种选择是使用 ON UPDATE CASCADE 选项配置外键,这样如果主键在父表上更新,它将级联到子表

暂无
暂无

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

相关问题 mysql:更新一个表的主键,该主键被另一个表称为外键 - mysql: update a table's primary key which is referred by another table as foreign key 使用另一个表的主键更新外键列 - Update foreign key column with another table's primary key MySQL - 将主键从一个表插入另一个表(外键) - MySQL - Inserting Primary Key from one table to another (Foreign Key) 如何用另一个表更新具有外键约束ON UPDATE RESTRICT的主键? - How to update primary keys which have foreign key constraint of ON UPDATE RESTRICT with another table? 如何删除表中的记录,该表的主键在另一个表中被引用为外键? - How do I delete a record in a table which has a primary key referenced in another table as foreign key? 如何在MySQL中更改表中的主键和表中的外键的列? - How to alter a column which is a primary key in the table and a foreign key in another table in MySQL? 如何使用表中的外键另一个表中的主键 - How to use a Foreign key in a table a primary key in another table 如何使用JPA引用的表的主键更新一个表中的外键? - How to update foreign key in one table with the primary key of the table that it references using JPA? 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key? 使用主键更新表,该表在许多子表中用作外键 - update table with primary key , which is used as foreign key in many child tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM