重命名 SQL Server 中的约束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8712875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:48:15  来源:igfitidea点击:

Rename a constraint in SQL Server?

sqlsql-serverconstraints

提问by mezamorphic

Is it possible to rename a constraint in SQL Server? I don't want to have to delete and create a new one because this constraint affects other already existing constraints and I will have to recreate/alter those.

是否可以重命名 SQL Server 中的约束?我不想删除并创建一个新的,因为这个约束会影响其他已经存在的约束,我将不得不重新创建/更改它们。

采纳答案by gbn

You can rename using sp_renameusing @objtype = 'OBJECT'

您可以重命名使用sp_rename使用@objtype = 'OBJECT'

This works on objects listed in sys.objects which includes constraints

这适用于 sys.objects 中列出的对象,其中包括约束

回答by ozz

After some more digging, I found that it actually has to be in this form:

经过更多的挖掘,我发现它实际上必须是这种形式:

EXEC sp_rename N'schema.MyIOldConstraint', N'MyNewConstraint', N'OBJECT'

Source

来源

回答by Mikael Eriksson

You can use sp_rename.

您可以使用sp_rename

sp_rename 'CK_Ax', 'CK_Ax1'

回答by rojib

answer is true :

答案是正确的:

exec sp_rename 
@objname = 'Old_Constraint',
@newname = 'New_Constraint',
@objtype = 'object'

回答by Winks

I know this is an old question, but I just found the following to be very helpful, in addition to the other great answers:

我知道这是一个老问题,但我发现除了其他很棒的答案之外,以下内容非常有帮助:

If the constraint to be renamed has a period in it (dot), then you need to enclose it in square brackets, like so:

如果要重命名的约束中有句点(点),则需要将其括在方括号中,如下所示:

sp_rename 'schema.[Name.With.Period.In.It]', 'New.Name.With.Period.In.It'