Field note
ch7
Relational Database Design
Outline:
- Features of Good Relational Design
- Atomic Domains and First Normal Form
- Decomposition Using Functional Dependencies
- Functional Dependency Theory
- Algorithms for Functional Dependencies
- Decomposition Using Multivalued Dependencies
- More Normal Forms
- Database-Design Process
- Modeling Temporal Data
Features of Good Relational Design
Consider combining relations
student(id, name, tot_cred)
stud_dept(id, dept_name)
student(id, name, tot_cred, dept_name)
假设我们有一个关系
inst_dept(id, name, salary, dept_name, building, budget)
我们应该怎么把它们拆分成两个关系(instructor和department)呢?
通过函数依赖我们可以看到
在 inst_dept 关系中,由于 dept_name 不是候选键,所以可以把 building, budget, dept_name 拿出来单独成一个关系 department,而 id, name, salary 作为 instructor
但是有时候这么做可能会导致信息丢失 lose information, 比如
employee(ID, name, street, citym, salary)
employee1(ID, name)
employee2(name, street, city, salary)
这就发生了信息丢失,因为可能会有同名的人,导致 employee1 和 employee2 之间的联系丢失了
Lossless-join Decomposition
我们说一个分解是lossless-join 的,如果我们通过 代替 , 不发生信息丢失
Conversely a decomposition is lossy if
!!! NOTE
more tuples implies more uncertainty(less information)
A decomposition of into and is lossless join if at least one of the following holds:
First Normal Form
A relation schema is in first normal form if the domains of all attributes of R are atomic
Domain is atomic if its elements are considered to be indivisible units.
一个关系满足第一范式需要以下条件:
- 原子性:关系中的每个属性都必须是原子值,即不可再分的基本数据单位。
- 无重复组:关系中的每一行都必须是唯一的,不能有重复的行。
- 唯一性:关系中的每一行都必须有一个唯一的标识符,即主键。
不符合第一范式的例子:
| 姓名 | 科目 |
|---|---|
| 张三 | 数学,物理 |
上边的“科目”一栏,就违背了“原子性”的原则
!!! Important
Normal Forms(NF):
$\mathrm{1NF} \rightarrow \mathrm{2NF} \rightarrow \mathrm{3NF} \rightarrow \mathrm{BCNF} \rightarrow \mathrm{4NF}$
Functional Dependencies
!!! NOTE
Let $R$ be a relation schema
$$
\alpha \subseteq R \ \mathrm{and} \ \beta \subseteq R
$$
The functional dependency $\alpha \rightarrow \beta$ holds on $R$ if for any two tuples $t_1$ and $t_2$ in $R$, if $t_1[\alpha] = t_2[\alpha]$, then $t_1[\beta] = t_2[\beta]$
函数依赖 要求从 到 的映射是唯一的。反过来就是要求从 到 的映射是唯一的
Example: Consider with the following instance of .
| 1 | 4 |
|---|---|
| 1 | 5 |
| 3 | 7 |
从 就不满足函数依赖,而从 就满足函数依赖
Superkey
K is a superkey for relation schema R if and only if
如果 K 是一个超键,那么 K 可以决定关系 R 中的所有属性 换句话说,K 可以唯一地标识关系 R 中的每一行
K is a candidate key for is and only if
- , and
- for no
如果 K 是候选键,当且仅当 K 能决定 R,并且没有任何 K 的子集能决定 R
A functional dependency is trivial if it is satisfied by all relations
Example:
in general,
Closure(闭包) of a set of Functional Dependencies
Given a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F.
For example: if and , then we can infer that
The set of all functional dependencies logically implied by F is the closure of F.
We donate the closure of F by , is a superset of F
!!! NOTE “Armstrong’s Axioms”
- if $\beta \subseteq \alpha$, then $\alpha \rightarrow \beta$ (<font color = blue>reflexivity, 自反率</font>)
- if $\alpha \rightarrow \beta$ then $\gamma \alpha \rightarrow \gamma \beta$ (<font color = blue>augmentation, 增补率</font>)
- if $\alpha \rightarrow \beta$ and $\beta \rightarrow \gamma$, then $\alpha \rightarrow \gamma$ (<font color = blue>transitivity, 传递率</font>)
!!! Example
$R \ = \ \{A, B, C, G, H, I\}$
$F \ = \ \{A \rightarrow B, A \rightarrow C, CG \rightarrow H, CG \rightarrow I, B \rightarrow H\}$
some members of $F^+$
- $A \rightarrow H$
- by transitivity from $A \rightarrow B$ and $B \rightarrow H$
- $AG \rightarrow I$
- by augmentation from $A \rightarrow C$ with G, to get $AG \rightarrow CG$ and then transitivity with $CG \rightarrow I$
- $CG \rightarrow HI$
- by augmentation from $CG \rightarrow I$ to infer $CG \rightarrow CGI$, and augmentation of $CG \tighrtarrow H$ to infer $CGI \rightarrow HI$, and then transitivity
Additional rules:
- If and holds, then holds (union, 合并)
- If holds, then holds and holds (decomposition, 分解)
- If and holds, then holds (pseudotransitivity, 伪传递率)
Closure of Attribute Sets
Given a set of attributes a, define the closure of a under F(donate by ) as the set of attributes that are functionally determined by a under F
!!! Example
$R(A, B, C, D), F \ = \ \{A \rightarrow B, B \rightarrow C, B \rightarrow D\}$
So
$$
A^+ = ABCD
$$
$$
B^+ = BCD
$$
$$
C^+ = C
$$
Uses of Attribute Closure

Canonical Cover(正则覆盖)
A canonical cover of F is a minimal set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies.
正则覆盖是 F 的一个最小超集,没有冗余的依赖关系或冗余的依赖关系部分
Example:
In , is redundant, so we can remove it and get
!!! Example
On RHS:
$$
\{A \rightarrow B, B \rightarrow C, A \rightarrow CD \}
$$
can be simplified to
$$
\{A \rightarrow B, B \rightarrow C, A \rightarrow D\}
$$
On LHS:
$$
\{A \rightarrow B, B \rightarrow C, AC \rightarrow D \}
$$
can be simplified to
$$
\{A \rightarrow B, B \rightarrow C, A \rightarrow D\}
$$
Extraneous Attributes(无关属性)
Consider a set F of functional dependencies and the functional dependency in F.
- Attribute A is extraneous in if and F logically implies
- 如果移除了 A 之后仍然能推导出相同的约束,那么 A 就是无关属性
- Attribute A is extraneous in if and the set of functional dependencies
!!! Example
Given $F \ = \ \{A \rightarrow C, AB \rightarrow C\}$
B is extraneous in $AB \rightarrow C$ because $\{A \rightarrow C, AB \rightarrow C\}$ logically implies $\{A \rightarrow C\}$
Given $F \ = \ \{A \rightarrow C, AB \rightarrow CD\}$
C is extraneous in $AB \rightarrow CD$ because $\{A \rightarrow C\}$ can be inferred even after deleting C.

Computing a Canonical Cover
- Combine and into
- Set is now
- A is extraneous in
- Check if the result of deleting A from is implied by the other dependencies
- Yes: in fact, is already in the set
- Set is now
- Check if the result of deleting A from is implied by the other dependencies
- C is extraneous in
- Check if is logically implied by and the other dependencies
- The canonical cover is
另一种方法是通过画图:

可以直观地看出正则覆盖是
!!! Example

Boyce-Codd Normal Form (BCNF)
一个关系模式满足 BCNF , 如果对于 的所有形如 的函数依赖,至少满足以下条件之一:
- 是平凡的
- 是关系模式的超键
即 BCNF 要求所有的函数依赖要么是平凡的,要么左侧是超键
Decomposing a Schema into BCNF
假设我们有 schema R 和一个非平凡的函数依赖
我们可以把 R 分解为:
BCNF Decomposition Algorithm

BCNF and Dependency Preservation
依赖保持:原来的函数依赖都可以在分解后的函数依赖中得到单独检验。如果需要把几个关系连在一起才能检验的,称为依赖不保持。
!!! NOTE
If it's sufficient to test only those dependencies on each individual relation of a decomposition in order to ensure that all functional dependencies hold, then that decomposition is <font color = blue>dependency perservation</font>
如果通过检验单一关系上的函数依赖,就能确保所有的函数依赖成立,那么这样的分解是依赖保持的。或者原来关系 R 上的每一个函数依赖,都可以在分解后的单一关系上得到检验或者推导得到。
Let be the set of all functional dependencies in that include only attributes in .(: the restriction of F on )
- A decomposition is dependency preserving if
- If it is not, then checking updates for violation of functional dependencies may require computing joins, which is expensive.
!!! Example

!!! Example

Third Normal Form (3NF)
A relation schema R is in third normal form(3NF) if for all:
at least ont of the following holds:
- is trivial
- is a superkey for R
- Each attribute A in is contained in a candidate key for R
任意一个非平凡的函数依赖,要么左边是超键,要么右边必须包含在一个候选键里面。

!!! NOTE “Algorithm”

Goals of Normalization
Let R be a relation scheme with a set F of functional dependencies.Decide whether a relation scheme R is in “good” form.
In the case that a relation scheme R is not in “good” form, decompose it into a set of relation scheme such that:
- each relation scheme is in good form(3NF or BCNF)
- the decomposition is a lossless-join decomposition
- Preferably, the decomposition should be dependency preserving