更改主键值

发布于 2024-11-27 20:33:49 字数 120 浏览 1 评论 0原文

我有10张桌子。每个表都被其他5个表的外键引用。

我需要更改这 10 个表的主键值。有什么方法可以更改它,以便它自动更改所有外键?

我正在使用 sql server 2008 并拥有管理工作室。

I have 10 tables. Each table referenced by foreign keys of other 5 tables.

I need to change the primary key value of those 10 tables. Is there any way to change it so that it will change automatically all the foreign keys?

I am using sql server 2008 and have the management studio.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终止放荡 2024-12-04 20:33:49

以下是如何使用 ON UPDATE CASCADE 外键选项执行此操作的示例。您感兴趣的部分是两个 ALTER TABLE 语句。

如果您使用 IDENTITY 列作为主键,那么这会变得更加困难,因为您无法更新 IDENTITY 列。

CREATE TABLE Parent
(
    ParentId INT NOT NULL CONSTRAINT [PK_Parent] PRIMARY KEY CLUSTERED,
    Name VARCHAR(10) NOT NULL
)

CREATE TABLE Child 
(
  ChildId INT NOT NULL CONSTRAINT [PK_Child] PRIMARY KEY CLUSTERED,
  ParentId INT NOT NULL CONSTRAINT [FK_Child_ParentId] FOREIGN KEY REFERENCES Parent (ParentId),
  Name VARCHAR(10) NOT NULL
)

INSERT INTO Parent (ParentId, Name) VALUES (1, 'Bob')
INSERT INTO Parent (ParentId, Name) VALUES (2, 'Sue')

INSERT INTO Child (ChildId, Name, ParentId) VALUES (1, 'Alice', 1)
INSERT INTO Child (ChildId, Name, ParentId) VALUES (2, 'Billy', 2)

SELECT * FROM Child 

-- Drop foreign key constraint and re-add 
ALTER TABLE Child 
  DROP CONSTRAINT [FK_Child_ParentId]

ALTER TABLE Child
 ADD CONSTRAINT [FK_Child_ParentId] 
    FOREIGN KEY (ParentId) REFERENCES Parent (ParentId) ON UPDATE CASCADE 

UPDATE Parent SET ParentId = ParentId + 100 

SELECT * FROM Child --shows the new ParentIds 

DROP TABLE Child 
DROP TABLE Parent 

Here is a sample how you can do it using the ON UPDATE CASCADE foreign key option. The part you'll be interested in are the two ALTER TABLE statements.

If you are using IDENTITY columns for your primary keys then this becomes more difficult as you can't update an IDENTITY column.

CREATE TABLE Parent
(
    ParentId INT NOT NULL CONSTRAINT [PK_Parent] PRIMARY KEY CLUSTERED,
    Name VARCHAR(10) NOT NULL
)

CREATE TABLE Child 
(
  ChildId INT NOT NULL CONSTRAINT [PK_Child] PRIMARY KEY CLUSTERED,
  ParentId INT NOT NULL CONSTRAINT [FK_Child_ParentId] FOREIGN KEY REFERENCES Parent (ParentId),
  Name VARCHAR(10) NOT NULL
)

INSERT INTO Parent (ParentId, Name) VALUES (1, 'Bob')
INSERT INTO Parent (ParentId, Name) VALUES (2, 'Sue')

INSERT INTO Child (ChildId, Name, ParentId) VALUES (1, 'Alice', 1)
INSERT INTO Child (ChildId, Name, ParentId) VALUES (2, 'Billy', 2)

SELECT * FROM Child 

-- Drop foreign key constraint and re-add 
ALTER TABLE Child 
  DROP CONSTRAINT [FK_Child_ParentId]

ALTER TABLE Child
 ADD CONSTRAINT [FK_Child_ParentId] 
    FOREIGN KEY (ParentId) REFERENCES Parent (ParentId) ON UPDATE CASCADE 

UPDATE Parent SET ParentId = ParentId + 100 

SELECT * FROM Child --shows the new ParentIds 

DROP TABLE Child 
DROP TABLE Parent 
真心难拥有 2024-12-04 20:33:49

您需要为这些外键设置 ON UPDATE CASCADE:

ALTER TABLE bar
ADD CONSTRAINT FK_foo_bar
FOREIGN KEY (fooid) REFERENCES foo(id)
ON UPDATE CASCADE

然后您只需更新 FK,并且引用字段也将作为事务的一部分进行更新:

UPDATE foo SET id = id + 1000

请注意,要更改约束,需要删除它们。

You need to set ON UPDATE CASCADE for those foreign keys:

ALTER TABLE bar
ADD CONSTRAINT FK_foo_bar
FOREIGN KEY (fooid) REFERENCES foo(id)
ON UPDATE CASCADE

Then you simply update the FKs and referring fields will also be updated as part of the transaction:

UPDATE foo SET id = id + 1000

Note that to alter constraints they need to be dropped.

梦太阳 2024-12-04 20:33:49

我自己从来没有这样做过,听起来这可能是一个坏主意。也就是说,我确实发现这篇文章介绍了执行此操作的两种方法:

http://support.microsoft。 com/kb/142480

一个使用存储过程和其他触发器。两者似乎都有点痛苦。

I have never done this myself, and it sounds like it might be a bad idea. That said, I did find this article which goes over two methods for doing this:

http://support.microsoft.com/kb/142480

One uses stored procs and the other triggers. Both seem like a bit of a pain.

指尖微凉心微凉 2024-12-04 20:33:49

IDENTITY 列的一个显着缺点是它无法直接更新。

解决方法是不在目标表中使用 IDENTITY,而是将其放在额外的表中。首先插入包含 IDENTITY 列的表,然后将生成的 IDENTITY 值插入到目标表中。

SQL Server 2012引入了与表无关的序列,这是对同一问题的更好的解决方案。序列不需要额外的表。

It's a significant disadvantage of an IDENTITY column that it can't be directly updated.

A workaround is not to use IDENTITY in the target table but put it in an extra table instead. Insert to the table with the IDENTITY column first, then insert the generated IDENTITY value to your target table.

SQL Server 2012 introduces table-independent Sequences, which are a better solution to the same problem. A sequence doesn't require the extra table.

不必你懂 2024-12-04 20:33:49

我知道这并不是您问题的真正答案,但我来到这里是在寻找如何简单地设置(写入、插入、更新或其他)PK(主键)列。

因此,您必须禁用 PK 约束,插入您的值,然后再启用它,更新将不起作用。

SET IDENTITY_INSERT IdentityTable ON
INSERT IdentityTable(TheIdentity, TheValue) VALUES (3, 'First Row')
SET IDENTITY_INSERT IdentityTable OFF

I know this is not really the answer to your question but I came here while looking for how to simply set (write, insert, update or else) a PK (primary key) column.

So you have to disable the PK constraint, insert your value, and enable it afterward, updates will not work.

SET IDENTITY_INSERT IdentityTable ON
INSERT IdentityTable(TheIdentity, TheValue) VALUES (3, 'First Row')
SET IDENTITY_INSERT IdentityTable OFF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文