Relational Databases: Table Key
关系数据库: 表键
A key, or index, as the term itself indicates, unlocks access to the tables. If you know the key, you know how to identify specific records and the relationships between the tables.
术语本身指示的键或索引可解锁对表的访问。如果你知道键,你就知道如何识别特定的记录和表之间的关系。
Each key consists of one or more fields, or field prefix. The order of columns in an index is significant. Each key has a name.
每个键由一个或多个字段或字段前缀组成。索引中的列顺序是显著的。每个键都有一个名称。
A candidate key is a field, or combination of fields, that uniquely identifies a record. It cannot contain a null value, and its value must be unique. (With duplicates, you would no longer be identifying a unique record).
候选键是唯一标识记录的字段或字段组合。它不能包含null值,并且其值必须是唯一的。(使用重复项,您将不再标识唯一记录)。
A primary key (PK) is a candidate key that has been designated to identify unique records in the table throughout the database structure.
主键 (PK) 是已指定用于在整个数据库结构中标识表中的唯一记录的候选键。
A surrogate key is a primary key that contains unique values automatically generated by the database system - usually, integer numbers. A surrogate key has no meaning, except uniquely identifying a record. This is the most common type of primary key.
代理键是包含由数据库系统自动生成的唯一值 (通常为整数) 的主键。除了唯一标识记录外,代理键没有任何意义。这是最常见的主键类型。
For example, see the following table:
例如,请参见下表:
Customer_code | First_name | Surname | Telephone_number |
---|---|---|---|
1 | Ariane | Edison | 448-2143 |
2 | Georgei | Edison | 448-2142 |
3 | Abbas | Edison | 9231-5312 |
At first glance, there are two possible candidate keys for this table.
乍一看,该表有两个可能的候选键。
Either customer_code or a combination of first_name, surname and telephone_number would suffice.
无论是customer_code 还是first_name、surname和telephone_number的组合都足够了。
It is always better to choose the candidate key with the least number of fields for the primary key, so you would choose customer_code in this example (note that it is a surrogate key).
最好为主键选择字段数最少的候选键,因此在此示例中,您可以选择customer_code (请注意,它是代理键)。
Upon reflection, there is also the possibility of the second combination not being unique.
经过思考,第二种组合也有可能不是独一无二的。
The combination of first_name, surname and telephone_number could in theory be duplicated, such as where a father has a son of the same name who is contactable at the same telephone number.
理论上,名字、姓氏和电话号码的组合可以重复,例如父亲有一个同名的儿子,可以用相同的电话号码联系。
This system would have to expressly exclude this possibility for these three fields to be considered for the status of primary key.
该系统必须明确排除将这三个字段考虑为主键状态的可能性。
There may be many Ariane Edisons, but you avoid confusion by assigning each a unique number.
可能有许多 Ariane Edisons,但是你可以通过给每个人分配一个唯一的数字来避免混淆。
Once a primary key has been created, the remaining candidates are labeled as alternate keys.
创建主键后,其余候选项将标记为备用键。