Rename a constraint in SQL Server?

SqlSql ServerConstraints

Sql Problem Overview


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 Solutions


Solution 1 - Sql

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

Solution 2 - Sql

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

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

Solution 3 - Sql

You can use sp_rename.

sp_rename 'CK_Ax', 'CK_Ax1'

Solution 4 - Sql

answer is true :

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

Solution 5 - Sql

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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmezamorphicView Question on Stackoverflow
Solution 1 - SqlozzView Answer on Stackoverflow
Solution 2 - SqlgbnView Answer on Stackoverflow
Solution 3 - SqlMikael ErikssonView Answer on Stackoverflow
Solution 4 - SqlrojibView Answer on Stackoverflow
Solution 5 - SqlWinksView Answer on Stackoverflow