Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique Constraint vs. Unique Index

Status
Not open for further replies.

rrhandle

Programmer
Dec 26, 2001
193
US
Can someone explain the difference between a Unique Constraint and a Unique Index, and which should be used when?

Thank you

 
A Unique Constraint uses a Unique Index to inforce Uniqueness.

IT just returns a slightly different error and as a constraint might be easier to document.....

For what that is worth


Rob
 
this is your main difference:
UNIQUE constraints can be defined on columns that allow null values, whereas PRIMARY KEY constraints can be defined only on columns that do not allow null values


When to use one over the other?
Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness, use a UNIQUE constraint instead of a PRIMARY KEY constraint when you want to enforce the uniqueness of:

A column, or combination of columns, that is not the primary key.
Multiple UNIQUE constraints can be defined on a table, whereas only one PRIMARY KEY constraint can be defined on a table.

A column that allows null values.

Denis The SQL Menace
SQL blog:
Personal Blog:
 
For SQL Server both do the same. Create unique constraint, and unique index will be created. Create unique index, no dupes will be allowed on indexed column(s). The only difference is semantical - "constraint" is more logical term, while "index" is it's physical implementation.

For some relational databases (early Watcom/Sybase comes to mind) that distinction made sense. There you could create unique constraint without index.

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Von
ill bite

<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>

???
 
Ya know what is that? :X

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top