SQL Server Unique Constraint

In SQL Server you can create a UNIQUE constraint on a field that is not the primary key.  The “CLUSTERED” keyword must be removed if you already have a clustered index on the table.  Here are some examples of a 1 column and 2 column unique constraint:


ALTER TABLE [dbo].[item_orders2] ADD CONSTRAINT
UNIQUE_Table2 UNIQUE CLUSTERED
(
order_id
) ON [PRIMARY]


ALTER TABLE [dbo].[item_orders3] ADD CONSTRAINT
UNIQUE_Table3 UNIQUE CLUSTERED
(
order_id,
item_id
) ON [PRIMARY]

 

Leave a comment