Akhil K Nambiar
Akhil K Nambiar

Reputation: 3975

Creating a unique constraint for 2 columns

I need to create a unique constraint based on two columns.

How do I achieve that in SQL Server 2008 Express edition?

Upvotes: 1

Views: 127

Answers (2)

njr101
njr101

Reputation: 9629

ALTER TABLE mytable
ADD CONSTRAINT myconstraint UNIQUE NONCLUSTERED (
  Col1,
  Col2
) ON [PRIMARY]

or if you prefer with an index:

CREATE UNIQUE NONCLUSTERED INDEX myindex ON mytable (
  Col1,
  Col2
) ON [PRIMARY]

Upvotes: 0

marc_s
marc_s

Reputation: 755471

ALTER TABLE dbo.YourTable
ADD CONSTRAINT YourConstraintName UNIQUE (Column1, Column2)

Upvotes: 2

Related Questions