如何级联重命名PK表,MS SQL 2005

时间:2022-06-27 01:57:24

I have successfully been able to rename a table and drop all constraints on that table with foreign key relationships and build they all back up. However, now I am at a point where the PK_tblFoo exists in more than one place (when I transfer the table to another DB). Renaming the table does not rename the primary key.

我已经成功地重命名了一个表,并使用外键关系删除该表上的所有约束,并将它们全部备份。但是,现在我处于PK_tblFoo存在于多个位置的位置(当我将表传输到另一个DB时)。重命名表不会重命名主键。

How would I cascade rename the primary key? I have renamed the table, I just need to get this portion figured out.

我如何级联重命名主键?我已经重命名了表格,我只需要弄清楚这一部分。

2 个解决方案

#1


1  

I believe I will need to this manually, drop all FK constraints, run this guy:

我相信我需要手动,放弃所有FK约束,运行这个人:

IF EXISTS ( SELECT  *
        FROM    sys.indexes
        WHERE   object_id = OBJECT_ID(N'[dbo].[tblFoo]')
                AND name = N'PK_tblBusinessListings' ) 
ALTER TABLE [dbo].[tblFoo] DROP CONSTRAINT [PK_tblBusinessListings]
GO
ALTER TABLE [dbo].[tblFoo]
ADD CONSTRAINT [PK_tblFoo_1] PRIMARY KEY CLUSTERED ( [ListingID] ASC )
    WITH ( PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,
           ONLINE = OFF ) ON [PRIMARY]

Then go through and set up all the FK constraint with the new PK name....errrgh....this is going to take a while.

然后通过新PK名称设置所有FK约束.... errrgh ....这将需要一段时间。

#2


0  

You could also use a refactoring tool, I know Visual Studio Team Edition for Database Professionals could handle this.

您也可以使用重构工具,我知道Visual Studio Team Edition for Database Professionals可以处理这个问题。

#1


1  

I believe I will need to this manually, drop all FK constraints, run this guy:

我相信我需要手动,放弃所有FK约束,运行这个人:

IF EXISTS ( SELECT  *
        FROM    sys.indexes
        WHERE   object_id = OBJECT_ID(N'[dbo].[tblFoo]')
                AND name = N'PK_tblBusinessListings' ) 
ALTER TABLE [dbo].[tblFoo] DROP CONSTRAINT [PK_tblBusinessListings]
GO
ALTER TABLE [dbo].[tblFoo]
ADD CONSTRAINT [PK_tblFoo_1] PRIMARY KEY CLUSTERED ( [ListingID] ASC )
    WITH ( PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,
           ONLINE = OFF ) ON [PRIMARY]

Then go through and set up all the FK constraint with the new PK name....errrgh....this is going to take a while.

然后通过新PK名称设置所有FK约束.... errrgh ....这将需要一段时间。

#2


0  

You could also use a refactoring tool, I know Visual Studio Team Edition for Database Professionals could handle this.

您也可以使用重构工具,我知道Visual Studio Team Edition for Database Professionals可以处理这个问题。