SQL Server - Ensuring uniqueness in one column depending on another column

Sorry if I get the terminology wrong. How to define a constraint in MSSQL Server 2005 to ensure uniqueness in one column depending on another column?

eg. given the last two columns:

1    A    1
2    A    2
3    A    2 <- Disallow because '2' has already appeared alongside 'A'
4    B    1
5    B    2
+3
source share
3 answers

Try the following:

CREATE TABLE tTable
    (field1 CHAR (1) NOT NULL,
     field2 INT NOT NULL,
     UNIQUE (field1, field2)
)
+7
source

Create a unique constraint for two columns?

, , , .

+1

It is not necessary to be the primary key, all that needs to be is a unique composite index.

0
source

All Articles