主键与聚集索引的关系

2021-12-26 00:00:00 sql database sql-server database-design

TABLE 可以有主键而没有聚集索引吗?

Can a TABLE have a primary key without a clustered index?

TABLE 是否可以有聚集索引而没有主键?

And can a TABLE have a clustered index without having a primary key?

谁能简单介绍一下主键和聚集索引的关系?

Can anybody briefly tell me the relationship between primary key and clustered index?

推荐答案

主键是一个逻辑概念 - 它是表中行的唯一标识符.因此,它有一堆属性——它不能为空,而且必须是唯一的.当然,由于您可能经常通过唯一标识符搜索记录,因此最好在主键上建立索引.

A primary key is a logical concept - it's the unique identifier for a row in a table. As such, it has a bunch of attributes - it may not be null, and it must be unique. Of course, as you're likely to be searching for records by their unique identifier a lot, it would be good to have an index on the primary key.

聚集索引是一个物理概念 - 它是一个影响记录在磁盘上存储顺序的索引.这使它在访问数据时成为一个非常快的索引,但如果您的主键不是序列号,它可能会减慢写入速度.

A clustered index is a physical concept - it's an index that affects the order in which records are stored on disk. This makes it a very fast index when accessing data, though it may slow down writes if your primary key is not a sequential number.

是的,您可以拥有一个没有聚集索引的主键 - 有时,您可能想要(例如,当您的主键是连接表上的外键组合,并且您不想招致写入时的磁盘洗牌开销).

Yes, you can have a primary key without a clustered index - and sometimes, you may want to (for instance when your primary key is a combination of foreign keys on a joining table, and you don't want to incur the disk shuffle overhead when writing).

是的,您可以在不是主键的列上创建聚集索引.

Yes, you can create a clustered index on columns that aren't a primary key.

相关文章