all groups > sql server (alternate) > october 2005 >
You're in the

sql server (alternate)

group:

Unique Constraint on Multiple columns


Unique Constraint on Multiple columns Dave
10/31/2005 2:42:28 PM
sql server (alternate): Can you create a unique constraint on multiple columns, or does it have
to be implemented as a unique index?

If possible can someone please post some sample code?

Thanks,
Re: Unique Constraint on Multiple columns Erland Sommarskog
10/31/2005 10:53:57 PM
Dave (daveg.01@gmail.com) writes:
[quoted text, click to view]

A UNIQUE constraint is always implemented as a unique index. But an
index can span more than one column.

[quoted text, click to view]

Here is a real-world table:

CREATE TABLE officerefunds (
orfid int NOT NULL,
chtcode aba_chtcode NOT NULL
CONSTRAINT ckc_orf_chtcode CHECK
(chtcode NOT IN ('MIN', 'MAX', 'CTX', 'STX')),
ofcid smallint NULL,
ityid smallint NULL,
insid aba_insid NULL,
chgid smallint NULL,
officerefund aba_fraction NOT NULL,
deductfee bit NOT NULL,
CONSTRAINT pk_orf PRIMARY KEY CLUSTERED (orfid),
CONSTRAINT ak_orf UNIQUE NONCLUSTERED
(chtcode, ofcid, ityid, insid, chgid),
CONSTRAINT ckt_orf_instrument CHECK
(NOT (ityid IS NOT NULL AND insid IS NOT NULL))
)



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
Re: Unique Constraint on Multiple columns Hugo Kornelis
10/31/2005 11:52:00 PM
[quoted text, click to view]

Hi Dave,

CREATE TABLE Example
(Col1 int NOT NULL,
Col2 int NOT NULL,
UNIQUE (Col1, Col2)
)

Best, Hugo
--

Re: Unique Constraint on Multiple columns Hugo Kornelis
11/2/2005 12:00:00 AM
[quoted text, click to view]

Hi Dave,

In SQL Server, a unique index will always be created forn each PRIMARY
KEY or UNIQUE constraint. This index is used to enforce the constraint.

Best, Hugo
--

Re: Unique Constraint on Multiple columns Dave
11/2/2005 8:36:48 AM
That is kind of what i though. So it is always an index? I could not
find that documentation anywhere, but I know I have read that some
where too.
AddThis Social Bookmark Button