Microsoft SQL Server 2008主键的含义 [英] Meaning of Primary Key to Microsoft SQL Server 2008
问题描述
主键的概念对SQL Server的数据库引擎有什么意义?我不是指在ID列上创建的聚集/非聚集索引,我的意思是约束对象主键。
What meaning does the concept of a primary key have to the database engine of SQL Server? I don't mean the clustered/nonclustered index created on the "ID" column, i mean the constraint object "primary key". Does it matter if it exists or not?
替代方法:
- alter table添加主键集群
- alter table create clustered index
/ p>
Does it make a difference?
推荐答案
通常,KEY是唯一标识表中每一行的列(或列的组合)。在表中可以有多个KEY(例如,您可能有一个 Person
表,其中社会安全号码和自动递增的数字都是KEY )。
In general, a KEY is a column (or combination of columns) that uniquely identifies each row in the table. It is possible to have multiple KEYs in a table (for example, you might have a Person
table where both the social security number as well as an auto-increasing number are both KEYs).
数据库设计者选择一个这些KEY为PRIMARY KEY。在概念上,它并不重要哪个 KEY被选择为PRIMARY KEY。然而,由于PRIMARY KEY通常用于从其他表(通过FOREIGN KEY)引用此表中的条目,因此选择一个良好的PRIMARY KEY可能与此相关。 (a)性能和(b)可维护性:
The database designer chooses one of theses KEYs to be the PRIMARY KEY. Conceptually, it does not matter which KEY is chosen as the PRIMARY KEY. However, since the PRIMARY KEY is usually used to refer to entries in this table from other tables (through FOREIGN KEYs), choosing a good PRIMARY KEY can be relevant w.r.t. (a) performance and (b) maintainability:
(a)由于通常使用主键在JOIN中,主键上的索引(其大小,其分布,...)与其他索引的性能相关性更强。
(a) Since the primary key will usually be used in JOINs, the index on the primary key (its size, its distribution, ...) is much more relevant to performance than other indexes.
主键在其他表中用作外键,因为更改主键值总是一个麻烦,因为其他表中的所有外键值都需要修改。
(b) Since the primary key is used as a foreign key in other tables, changing the primary key value is always a hassle, since all the foreign key values in the other tables need to be modified as well.
这篇关于Microsoft SQL Server 2008主键的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!