关系数据库中的范式

准备面试,整理一下范式相关
部分NF定义参考于 https://www.zhihu.com/question/24696366/answer/29189700 <-这个答案中对于2NF/3NF定义中的码我认为应该是主码而非码(候选码), 其他的表述还是可以参考的.
英文定义部分参考的数据库课上的课件

各种NF:

1. 1NF

Domain is atomic,不可再分
A relational schema R is in first normal form if the domains of all attributes of R are atomic.

2. 2NF

条件1: 满足1NF
条件2: 非主属性完全依赖于主键, 即不存在非主属性部分依赖于主键.<-非主属性指不包含在任何候选键中的一个属性
部分依赖举例: R{A,B,C}, 主键为(AB), 存在A->C, B->C, 此时即为部分依赖.
必须是AB->C才是完全依赖

3. 3NF

条件1: 满足2NF
条件2: 非主属性不传递依赖于主键
A relation schema R is in 3rd normal form (3NF) if for all α → β in F+, at least one of the following holds:

  • α → β is trivial (i.e., β ∈ α)
  • α is a superkey for R
  • Each attribute A in β – α is contained in a candidate key for R. (A-B means union belongs to A but not to B). (NOTE: each attribute may be in a different cand. key)
4. BCNF

条件1: 满足2NF
条件3: 每个属性(含主属性)都不传递依赖于主键
非主键传递依赖于主键举例: R{A,B,C}, A为主键, 存在A->B, B->C, 此时非主键C传递依赖于主键A
A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form (α → β) where α ⊆ R and β ⊆ R, at least one of the following holds:

  • α → β is trivial (i.e., β ⊆ α)
  • α is a superkey for R
    BCNF的限制要低于高于3NF. BCNF一定是3NF

Functional Dependencies 函数依赖

1. 函数依赖

X->Y
不存在X1=X2时, Y1!=Y2.

2. 完全(函数)依赖

X->Y, 且不存在(X的真子集)->Y, 称Y完全依赖于X;

3. 部分(函数)依赖

X->Y, 且存在(X的真子集)->Y, 称Y部分依赖于X;

4. 传递(函数)依赖

Y不属于X && Y!->X, X->Y, Y->Z, 称Z传递依赖于X.
(http://blog.csdn.net/crave_shy/article/details/12401935)

5. trivial function

α → β is trivial if β ⊆ α.
Example: ID, name → ID; name → name


各种键

Key:

关系中的某个属性或者某几个属性的组合,用于区分每个元组(tuple)(每一行)

Super key:

超键(super key): 在关系中能唯一标识元组的属性集称为关系模式的超键
K is a superkey for relation schema R if and only if K → R
超键中可以包含非候选键.

Candidate key:

K is a candidate key for R if and only if

  • K → R, and
  • for no α ⊂ K, α → R

候选键(candidate key): 不含有多余属性的超键称为候选键, 即实现了候选键之外的其余属性对于候选键的完全依赖.
候选键可能有多个, 一个候选键可能包含一个或多个属性.

Primary key:

主键(primary key):用户选作元组标识的一个候选键作为程序主键
主键只有一个, 主键可能包含一个或多个属性.

Foreign key:

外键(foreign key): 如果关系模式R1中的某属性集不是R1的主键,而是另一个关系R2的主键则该属性集是关系模式R1的外键。