Ali Hussain
Ali Hussain

Reputation: 896

In SQL can a table have both primary keys + composite keys?

In SQL Server, can a table have primary key on 1 column and then composite keys on other two or more columns?

Upvotes: 0

Views: 2358

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89406

Yes.

A table can have more than one Key, and a Key has one or more key columns.

In SQL Server you create a Key with any of a UNIQUE CONSTRAINT, a PRIMARY KEY CONSTRAINT, or a UNIQUE INDEX. A table can at most one PRIMARY KEY CONSTRAINT, but can have any number of UNIQUE CONSTRAINTs or UNIQUE INDEXs.

So yes, a table can have a PRIMARY KEY on one column, and a composite UNIQUE INDEX or UNIQUE CONSTRAINT.

Upvotes: 5

Related Questions