<body >

How to Rename a Database Constaint

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.

Labels: ,

» Post a Comment
« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
 
   



Blogger Template by Gecko & Fly, modified by yours truly.