How to Rename a Database Constraint

The SQL Server BOL lacks the syntax for renaming constraints, and most of the code samples on the internet flat out don’t work. By trial and error, I discovered the correct syntax:

CREATE TABLE foo (  bar INT,  CONSTRAINT foo_unique_bar UNIQUE (bar))

EXEC sp_rename 'foo_unique_bar', 'foo_unique_bar2', 'object'

Note that you do not use the table’s name in the first parameter to sp_rename. This is contrary to how sp_rename usually works, so it’s an effective monkey wrench.

Leave a Reply

Your email address will not be published. Required fields are marked *


*