Rename a constraint in SQL Server?

两盒软妹~` 提交于 2019-11-27 13:24:42

问题


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.


回答1:


You can rename using sp_rename using @objtype = 'OBJECT'

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




回答2:


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




回答3:


You can use sp_rename.

sp_rename 'CK_Ax', 'CK_Ax1'



回答4:


answer is true :

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



回答5:


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'


来源:https://stackoverflow.com/questions/8712875/rename-a-constraint-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!