Reputation: 10672
I am just learning about Indexing in SQL server but got confuse between Clustered and Unique index. if both are applied on a unique key column ex: PersonID. so what is difference between both.
Thanx.
Upvotes: 19
Views: 48055
Reputation: 432667
The two are unrelated:
You can have all 4 permutations:
Some confusion occurs because the default for a "primary key" (PK) in SQL Server is clustered.
A "primary key" must always be unique though. The difference between "unique" and "PK" is that unique allows one NULL, PK doesn't allow any NULLs.
Finally, some number limits
Upvotes: 45
Reputation: 7215
One crude way of thinking about it is to think of a phone book. The clustered index is the order the pages are written in. Any other indexes are separate lists showing which page to go.
For example a phone book is “clustered” on surname but you might also want to lookup by street so you would have a separate list saying people that live on fake street are on pages 3,45 and 63 etc
Upvotes: 10
Reputation: 9740
A unique index is just an index with a unique constraint, nothing more, nothing less. A clustered index orders the data phsyically on the disk to match the index. It is useful if you access the data in the table very often via just one column, e.g. via the primary key. Also a table can have only one clustered index (obvious, of course).
Upvotes: 22
Reputation: 14874
AFAIK every table can have just one clustered index that is the primary key usually, but it may have m any unique indexes.
Upvotes: 1