Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 1.27 KB

alter-existing-constraint.md

File metadata and controls

27 lines (19 loc) · 1.27 KB

Alter Existing Constraint in Transact-SQL

First, see this quote from Modify Unique Constraints:

To modify a UNIQUE constraint using Transact-SQL, you must first delete the existing UNIQUE constraint and then re-create it with the new definition.

This constraint (totally pun intended) extends to all sorts of constraints and all sorts of relational databases, by the way - NOT just unique constraints as the quote suggests, and not just SQL Server. You just can't modify existing constraints in place.

So, you can drop the constraint using this:

ALTER TABLE myTable
DROP CONSTRAINT myConstraint;

Once the constraint has been dropped, add the unique constraint with the additional column or whatever you wanted to do:

ALTER TABLE myTable
ADD CONSTRAINT myConstraint UNIQUE (column1, column2);