如何在SQL Server中重命名主键约束

时间:2022-07-27 09:02:56

I have a PK constraint on table Notes named PK_dbo.Notes and want to rename it to PK_Notes using SQL Server DDL, i.e. not by using SSMS rename menu option.

我对名为PK_dbo.Notes的表Notes有PK约束,并希望使用SQL Server DDL将其重命名为PK_Notes,即不使用SSMS重命名菜单选项。

Mentioned in another question's answers queries don't work for me. That thread's answers are also helpful, but don't work too.

在另一个问题的答案中提到查询对我不起作用。该主题的答案也很有帮助,但也不行。

1 个解决方案

#1


17  

Sometimes you need to explicitly wrap names in square brackets, like this:

有时你需要在方括号中明确地包装名称,如下所示:

sp_rename @objname = N'[Notes].[PK_dbo.Notes]', @newname = N'PK_Notes'

I think it's because of the dot in PK name.

我认为这是因为PK名称中的点。

Also, as you see, PK constraints don't need @objtype = 'OBJECT' to be specified.

另外,如您所见,PK约束不需要指定@objtype ='OBJECT'。

#1


17  

Sometimes you need to explicitly wrap names in square brackets, like this:

有时你需要在方括号中明确地包装名称,如下所示:

sp_rename @objname = N'[Notes].[PK_dbo.Notes]', @newname = N'PK_Notes'

I think it's because of the dot in PK name.

我认为这是因为PK名称中的点。

Also, as you see, PK constraints don't need @objtype = 'OBJECT' to be specified.

另外,如您所见,PK约束不需要指定@objtype ='OBJECT'。