Re: Unique Constraint on Multiple columns
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Mon, 31 Oct 2005 22:53:57 +0000 (UTC)
Dave (daveg.01@xxxxxxxxx) writes:
> Can you create a unique constraint on multiple columns, or does it have
> to be implemented as a unique index?
A UNIQUE constraint is always implemented as a unique index. But an
index can span more than one column.
> If possible can someone please post some sample code?
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@xxxxxxxxxxxxx
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
.
- Follow-Ups:
- Re: Unique Constraint on Multiple columns
- From: Dave
- Re: Unique Constraint on Multiple columns
- References:
- Unique Constraint on Multiple columns
- From: Dave
- Unique Constraint on Multiple columns
- Prev by Date: Re: Unique Constraint on Multiple columns
- Next by Date: Re: Stored Procs and db_owner
- Previous by thread: Re: Unique Constraint on Multiple columns
- Next by thread: Re: Unique Constraint on Multiple columns
- Index(es):
Relevant Pages
|