gotchasqlMinor
What is difference between is_unique column and is_unique_constraint columns in sys.indexes
Viewed 0 times
is_uniquewhatcolumncolumnsindexesdifferencebetweensysis_unique_constraintand
Problem
We have two columns
-
When
And when
Why is that so?
-
Where we use unique index and where unique constraint, what is best practice?
is_unique and is_unique_constraint for unique constraints in sys.indexes and I want to clarify few concepts based on these two columns - Create statement for both types of indexes are same except
fill factorproperty value or do I missing something?
- If statement 1 is true then why we have two columns in sys.indexes for unique constraint?
-
When
is_unique=1 is_unique_constraint =0 and then drop statement for index isDROP INDEX [index name] ON [dbo].[TableName] WITH ( ONLINE = OFF ) And when
is_unique=1 is_unique_constraint =1 and then drop statement for index isALTER TABLE [dbo].[TableName] DROP CONSTRAINT [IndexName] Why is that so?
-
Where we use unique index and where unique constraint, what is best practice?
Solution
Fact is that there is no practical difference between a unique constraint and a unique index.
As best practice, If uniqueness is required then always apply unique constraint, instead of creating only unique index. This way you can't drop a unique index accidentally.Also this way you can document all applied constraints easily.
http://technet.microsoft.com/en-us/library/aa224827(v=sql.80).aspx
- Querying
sys.objects, you will find unique constraint is listed as a constraint object and its related index can be found while queryingsys.indexes, where it is markedis_unique = 1as well asis_unique_constraint = 1. On other hand, for a unique indexesis_unique_constraintvalue will be zero (0).
- While creating unique constraint you can use Fill Factor option as compare to unique index where you have more options like
FILLFACTOR, PAD_INDEX, IGNORE_DUP_KEY, DROP_EXISTING, and STATISTICS_NORECOMPUTE.
- Unique constraint is maintained through a unique index
- Just like Default and Primary Key, a unique constraint can't be disabled using NOCHECK syntax
- To delete a unique index with
is_unique_constraint = 1, it is must to delete it through DROP CONSTRAINT syntax instead of DROP INDEX because such index is associated with a unique constraint (object), so one must drop CONSTRAINT and index will be dropped automatically.
As best practice, If uniqueness is required then always apply unique constraint, instead of creating only unique index. This way you can't drop a unique index accidentally.Also this way you can document all applied constraints easily.
http://technet.microsoft.com/en-us/library/aa224827(v=sql.80).aspx
Context
StackExchange Database Administrators Q#82035, answer score: 4
Revisions (0)
No revisions yet.