Re: Difference between creating a Unique Constraint and a Unique Index
From: Wayne Snyder (wayne.nospam.snyder_at_mariner-usa.com)
Date: 11/04/04
- Next message: Clark Wilson: "Re: what is foo and foobar ?"
- Previous message: Paul Sjoerdsma: "Difference between creating a Unique Constraint and a Unique Index"
- In reply to: Paul Sjoerdsma: "Difference between creating a Unique Constraint and a Unique Index"
- Next in thread: AnthonyThomas: "RE: Difference between creating a Unique Constraint and a Unique Index"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 4 Nov 2004 07:59:14 -0500
Yes, a unique index enforces uniqueness. A unique index is created in the
background when you specify a PK or Unique constraint.
The difference is more semantic, but also tactical... Placing the constraint
indicates a design requirement. When you create the index directly, how can
you know IF the index was created for performance, or if the index is
intended to enforce some design constraint... The fact is that you can
not... So use PK and Uniques to enforce design requirements...Indexes are
created for 2 main reasons, to enforce integrity and speed up queries.The
indexes used to speed up queries( or other statements) may need to change
over time as business priorities change, but we would still need to maintain
the indexes created to enforce data integrity.
Indexes created as a by-product of PK or unique constraints can NOT be
dropped using DROP index, so you are protected from inadvertently dropping
an index which is used for data integrity. FOr this you must use alter table
drop constraint, alter table add constraint.... Any index that goes away
using DROP index, therefore, would have been created for performance, and is
a candidate for re-assessment as needs change
Additionally FKs must be placed on columns with PKs or Unique
constraints.(from BOL). HOwever the actual code allows you to put an FK on
any column which has a unique index ( although I beleive this should NOT be
allowed)..
Hope this helps/
-- Wayne Snyder, MCDBA, SQL Server MVP Mariner, Charlotte, NC www.mariner-usa.com (Please respond only to the newsgroups.) I support the Professional Association of SQL Server (PASS) and it's community of SQL Server professionals. www.sqlpass.org "Paul Sjoerdsma" <paul@pasoftware.nl> wrote in message news:2j8ko0pdll18amito60gcqne5g58en6otj@4ax.com... > Hi, > > I've looked at the online documentation and encountered this piece of > text > > "If uniqueness must be enforced to ensure data integrity, create a > UNIQUE or PRIMARY KEY constraint on the column rather than a unique > index" > > I'm baffled; I understand a unique contraint, and personally do not > care how it is physically enforced (using an index to speed up the > process) as long as it is enforced. However what is the deal with > unique indexes? The piece of documentation above seems to suggest that > a unique index will not enforce uniqueness. > > The reason I ask is that I am working against a legacy system where > unique indexes are used for each table. Each table, besides a PK, has > a column that stores a GUID. Personally I would use a Unique > Contraint, but they have used a unique index on this column. What > would be the advantages and disadvantages of using a unique index vs a > unique constraint.?? > > > regards > Paul Sjoerdsma >
- Next message: Clark Wilson: "Re: what is foo and foobar ?"
- Previous message: Paul Sjoerdsma: "Difference between creating a Unique Constraint and a Unique Index"
- In reply to: Paul Sjoerdsma: "Difference between creating a Unique Constraint and a Unique Index"
- Next in thread: AnthonyThomas: "RE: Difference between creating a Unique Constraint and a Unique Index"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|