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.
我刚刚学习SQL服务器中的索引,但在Clustered和Unique索引之间混淆了。如果两者都应用于唯一键列ex:PersonID。那么两者之间有什么区别。
Thanx.
感谢名单。
4 个解决方案
#1
15
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).
唯一索引只是一个具有唯一约束的索引,仅此而已。聚簇索引在磁盘上以逻辑方式对数据进行排序以匹配索引。如果您经常只通过一列访问表中的数据,这将非常有用,例如:通过主键。另外一个表只能有一个聚簇索引(当然很明显)。
#2
34
The two are unrelated:
这两者是无关的:
- "Unique" ensures each value occurs exactly once only
- “Unique”确保每个值仅发生一次
- "Clustered" is how the data is arranged on disk
- “Clustered”是数据在磁盘上的排列方式
You can have all 4 permutations:
您可以拥有所有4种排列:
- "unique non-clustered"
- “独特的非群集”
- "unique clustered"
- “独特聚集”
- "non-unique non-clustered"
- “非独特的非群集”
- "non-unique clustered"
- “非独特群集”
Some confusion occurs because the default for a "primary key" (PK) in SQL Server is clustered.
由于SQL Server中“主键”(PK)的默认值已群集,因此会出现一些混淆。
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.
但是,“主键”必须始终是唯一的。 “unique”和“PK”之间的区别在于unique唯一允许一个NULL,PK不允许任何NULL。
Finally, some number limits
最后,一些数字限制
- because clustered refers to the on disk layout, you can have only one clustered index per table
- 因为clustered指的是磁盘布局,每个表只能有一个聚簇索引
- a table can't have more than one pimary key but can have many unique indexes
- 一个表不能有多个pimary键,但可以有许多唯一索引
#3
9
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
例如,电话簿在姓氏上“聚集”,但您可能还想通过街道查找,因此您将有一个单独的列表,说生活在假街上的人在第3,45和63页等
#4
1
AFAIK every table can have just one clustered index that is the primary key usually, but it may have m any unique indexes.
AFAIK每个表通常只有一个聚簇索引,它通常是主键,但它可能有任何唯一索引。
更多:http://decipherinfosys.wordpress.com/2007/07/04/back-to-the-basics-difference-between-primary-key-and-unique-index/
#1
15
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).
唯一索引只是一个具有唯一约束的索引,仅此而已。聚簇索引在磁盘上以逻辑方式对数据进行排序以匹配索引。如果您经常只通过一列访问表中的数据,这将非常有用,例如:通过主键。另外一个表只能有一个聚簇索引(当然很明显)。
#2
34
The two are unrelated:
这两者是无关的:
- "Unique" ensures each value occurs exactly once only
- “Unique”确保每个值仅发生一次
- "Clustered" is how the data is arranged on disk
- “Clustered”是数据在磁盘上的排列方式
You can have all 4 permutations:
您可以拥有所有4种排列:
- "unique non-clustered"
- “独特的非群集”
- "unique clustered"
- “独特聚集”
- "non-unique non-clustered"
- “非独特的非群集”
- "non-unique clustered"
- “非独特群集”
Some confusion occurs because the default for a "primary key" (PK) in SQL Server is clustered.
由于SQL Server中“主键”(PK)的默认值已群集,因此会出现一些混淆。
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.
但是,“主键”必须始终是唯一的。 “unique”和“PK”之间的区别在于unique唯一允许一个NULL,PK不允许任何NULL。
Finally, some number limits
最后,一些数字限制
- because clustered refers to the on disk layout, you can have only one clustered index per table
- 因为clustered指的是磁盘布局,每个表只能有一个聚簇索引
- a table can't have more than one pimary key but can have many unique indexes
- 一个表不能有多个pimary键,但可以有许多唯一索引
#3
9
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
例如,电话簿在姓氏上“聚集”,但您可能还想通过街道查找,因此您将有一个单独的列表,说生活在假街上的人在第3,45和63页等
#4
1
AFAIK every table can have just one clustered index that is the primary key usually, but it may have m any unique indexes.
AFAIK每个表通常只有一个聚簇索引,它通常是主键,但它可能有任何唯一索引。
更多:http://decipherinfosys.wordpress.com/2007/07/04/back-to-the-basics-difference-between-primary-key-and-unique-index/