如何选择我的主键?
我在选择主键找到了此阅读材料。
I found this reading material on choosing a primary key.
- 有关如何为给定表选择主键的指南/博客文章?
- 我应该使用自动递增/生成的键,还是应该将主键基于正在建模的数据(假设它具有真正唯一的字段)?
- 为了性能原因,主键永远长久,或者我可以将外部唯一ID作为主键,即使它是一个字符串吗?
I believe that in practice using a natural key is rarely better than a surrogate key.
以下是使用自然金钥作为主键的主要缺点:
The following are the main disadvantages of using a natural key as the primary key:
-
您可能键值不正确,或者您可能只想重命名键值。要编辑它,您必须更新所有使用它作为外键的表。
You might have an incorrect key value, or you may simply want to rename a key value. To edit it, you would have to update all the tables that would be using it as a foreign key.
通常很难有一个真正的独特的自然关键字。
It is often difficult to have a truly unique natural key.
自然键通常是字符串。数字字段上的索引比字符串字段上的索引要紧凑得多。
Natural keys are often strings. An index on an numeric field will be much more compact than one on a string field.
主键的数据类型应该是什么。数字键通常表现更好,但你可以使用字符串,特别是如果表不大,引用它的表也不大。
There is no hard rule on what the data type of the primary key should be. A numeric key normally performs better, but you could use a string, especially if the table is not big, and the tables that reference it are not big either.