数据库理论基本概念之关系键
数据库理论基本概念之关系键
关系键
维基百科,自由的百科全书
关系键 是 的重要组成部分。关系键是一个表中的一个或几个属性,用来标识该表的每一行或与另一个表产生联系。
目录
[1
主键]( )
[2
超键]( )
[3
候选键]( )
[4
外键]( )
[5
代理键]( )
[5.1
何时使用代理键]( )
[6
自然键]( )
[7
外部链接]( )
主键
数据库理论中, 超键、候选键与主键之间的关系:
• 候选键是超键的一种:不含有多余属性的超键
• 某个候选键被选为主键
,又称 主码 ( : primary key或unique key )。 中对储存数据对象予以唯一和完整标识的 或属性的组合。一个数据列只能有一个主键,且主键的取值不能缺失,即不能为 (Null)。
从技术的角度来看,primary key和unique key有很多相似之处。但还是有以下区别:
- 作为primary key的域/域组不能为Null。而unique key可以。
- 在一个表中只能有一个primary key,而多个unique key可以同时存在。
- 更大的区别在逻辑设计上。primary key一般在逻辑设计中用作记录标识,这也是设置primary key的本来用意。而unique key只是为了保证域/域组的唯一性。
超键
超键( : superkey ),有的文献称“超码”,是在数据库 设计中能够唯一标示 的属性集。
包含 所有 属性的集叫做 超键 。
候选键
在 中, 候选键 或 候选码 ( : candidate key )是某个 的一组属性所组成的 ,它需要同时满足下列两个条件:
- 这个属性集合始终能够确保在关系中能唯一标识
- 在这个属性集合中找不出合适的 能够满足条件(1)
满足第一个条件的属性集合称为 ,因此我们也可以把候选键定义为“最小超键”,也就是不含有多余属性的超键。
候选键的重要性是它们能够在关系中唯一标识出不同的元组,因此超键也是在设计 时需要指定的最重要的约束之一。由于在关系模型中,每个关系都是一个集合(没有重复的元素),所以每个关系都至少有一个候选键(因为所有属性组合必然是个超键)。但是在某些 中 也能代表 ,所以在每个关系中都显式地定义至少一个候选键是一条很重要的设计原则。 通常都需要将每个关系中的某个候选键定义为 ,亦即这个候选键是区分不同元组时首选的识别方式,例如 通常就是引用 而非其他候选键。
外键
外键 ( : foreign key ,台湾译作 外来键 ),又称 外部键 。其实在关系数据库中,每个数据表都是由关系来连系彼此的关系,父数据表( Parent Entity )的 ( primary key )会放在另一个数据表,当做属性以创建彼此的关系,而这个属性就是外键。
比如,学生跟老师之间是教学的关系,学生数据表会有个属性叫指导老师(FK),而这个值就是对应到老师数据表的老师代号(PK),学生的指导老师就是外键。
代理键
在 设计中, 代理键 是在当数据表中的候选键都不适合当主键时,例如数据太长,或是意义层面太多,就会请一个无意义的但唯一的字段来代为作主键。
代理键是:
Surrogate (1) – Hall, Owlett and Codd (1976): 一个代理键值确定了外部世界的一个实体。代理键值是数据库生成的,从来不显示给用户或应用程序看。 Surrogate (2) – Wieringa and De Jonge (1991): 一个代理键值确定了数据库中的一个对象。代理键值是数据库生成的,用户或应用程序看不到它。
在实践中,代理键值通常是个自动递增的数字。在Sybase或SQL Server,用identity column标识代理键,PostgreSQL里用serial,Oracle里用SEQUENCE,在MySQL里用一个标记有AUTO_INCREMENT的字段。
何时使用代理键
以中国大陆的十八位 为例,从左往右为六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
一家公司想要将它的客户记入数据库,以客户的身份证号作为主键当然是可以的;但是这18位身份证号是用于标识大陆13多亿人口的,一家公司的客户显然没有这么多,所以用18位的数字作为主键有点浪费空间。
另外,身份证号中包含了地区、生日信息,若以身份证号为主键,要不要另开字段记录客户的地区、生日也是个问题。如果不另开字段,从主键(身份证号)中提取地区、生日有点麻烦;如果开字段,主键和地区、生日字段的数据存在冗余。
所以,一般的做法是,根本不记录客户的身份证号(除非有其他需求),用一个代理键作为主键,另开字段记录客户的地区、生日等信息。
自然键
自然键与代理键相反,它是在自然生活中唯一确定一个事物的标识。身份证号就是一个自然键,用于确定一个人。
外部链接
(DBMS) ( · · ) | |
概念 · · · · · · ( · · · ) · · · · · · · · · · · | |
数据库组件 · · · · · · · · · · | 分类: - - - 指令: · · · · · · · · · · · · · 安全: · |
数据库管理系统的实现 | |
实现类型 · · · · · · · · · | |
数据库产品 ( ) · ( ) | 数据库组件 · · · · · · |
:
======================================================================
Natural key
From Wikipedia, the free encyclopedia
In design, a natural key is a key that is formed of attributes that already exist in the real world. For example, a USA citizen’s social security number could be used as a natural key. In other words, a natural key is a that has a logical relationship to the attributes within that . A natural key is sometimes called domain key .
The main advantage of a natural key over a , which has no meaning outside the database environment, is that it already exists; there is no need to add a new, artificial to the . Using a natural key (when one can be identified) also simplifies data quality: It ensures that there can only be one row for a key; this “one version of the truth” can be verified, because the natural key is based on a real-world observation.
The main disadvantage of choosing a natural key is that its value may change and the relational database engine may not be able to propagate that change across the related . For example, if person_name is used as the primary key for the person table, and a person gets married and changes name, then all of the related tables need to be updated also. The secondary disadvantage of choosing a natural key is identifying . The primary key must consist of the attributes that uniquely identify a row. However, it may be difficult (or it may add constraints) to create a natural key on a table. For example, if person_name is used as a primary key for the person table, many persons may share the same name and all but the first entry will be rejected as a duplication. The uniqueness constraint may be overcome by adding an additional column to the primary key, like street_address , to increase the likelihood of uniqueness.
External links
, B Carter
.
, Near Infinity , Create Data Disaster
.
, c2
.
: