Normalisation
引入
6:35
数据库
数据库是关于某个领域的一组真实陈述。这门课介绍如何建模、查询、保护和治理数据。前三个单元介绍如何建模和组织数据:关系设计、SQL 和规范化。接下来的两个单元介绍如何利用索引和事务,让数据访问快速且安全。最后一个单元介绍数据治理。这里的每条查询都已在文档记录的示例模式上执行过。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| insert anomaly/ˈɪnsɜːt əˈnɒməli/ | 插入异常 | chā rù yì cháng |
| update anomaly/ˈʌpdeɪt əˈnɒməli/ | 更新异常 | gēng xīn yì cháng |
| delete anomaly/dɪˈliːt əˈnɒməli/ | 删除异常 | shān chú yì cháng |
| functional dependency/ˈfʌŋkʃənl dɪˈpendənsi/ | 函数依赖 | hán shù yī lài |
| Second normal form/ˈsekənd ˈnɔːml fɔːm/ | 第二范式 | dì èr fàn shì |
| third normal form/θɜːd ˈnɔːml fɔːm/ | 第三范式 | dì sān fàn shì |
| Boyce-Codd normal form/bɔɪs kɒd ˈnɔːml fɔːm/ | BC范式 | BC fàn shì |
| denormalisation/dɪˌnɔːməlaɪˈzeɪʃn/ | 反规范化 | fǎn guī fàn huà |
| lossless/ˈlɒsləs/ | 无损 | wú sǔn |
| spurious rows/ˈspjʊərɪəs rəʊz/ | 伪元组 | wěi yuán zǔ |
| attribute closure/ˈætrɪbjuːt ˈkləʊʒə/ | 属性闭包 | shǔ xìng bì bāo |
The anomalies
- A table that is not normalised causes three problems. An insert anomaly 插入异常: you cannot add a course that has no student yet.
- An update anomaly 更新异常: one fact must be changed in many rows. A delete anomaly 删除异常: deleting the last row that holds a fact loses the fact.
- The cause in every case is a fact that is stored more than once.

Changing a course title in 14 enrollment rows is —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
One fact stored many times: the update anomaly.
Dependencies drive the split
- A functional dependency 函数依赖 $A \to B$ means each value of A fixes one value of B. Second normal form 第二范式 removes dependencies on part of the key; third normal form 第三范式 removes transitive ones (through another column); Boyce-Codd normal form BC范式 makes every determinant a key.
- In
Enroll(student, course, title, dept, building),course → titledepends on part of the key, andtitle → dept → buildingis transitive. Split it into Course, Dept and Enroll: each fact is stored once, and every anomaly is gone.
What is the lowest normal form violated by a non-prime attribute depending on part of a candidate key?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
2NF removes dependencies on part of the key; 3NF the transitive ones.
Put the steps of normalising a table in order.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Each successive normal form adds constraints; identify the dependencies before decomposing the table.
When to stop
Room(building, room, capacity, projector_type), where all rooms in a building have the same projector type:building → projector_typehas a determinant that is not a key. Split offBuilding(building, projector_type).- A stored running total can be computed from other data, but computing it again for every view may cost more than the risk of an anomaly.
- Controlled denormalisation 反规范化 with a refresh trigger is fine once you understand the normal form. Normalise to reason; denormalise when measurements show the need.
A non-key column determining a non-prime column breaks —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A transitive chain through a non-key determinant is the 3NF violation.
Lossless splits
- A split must be lossless 无损: joining the parts gives back exactly the original rows. For a split into two parts, the shared columns must be a superkey of at least one part.
Enroll(student, course)andCourse(course, title, dept)sharecourse, the key of Course, so the join is exact. - Split on
titleinstead, into(student, title)and(course, title), and two courses with the same title join into spurious rows 伪元组 that were never there. 3NF can always be reached losslessly while keeping every dependency; a lossless BCNF decomposition can always be constructed but can lose a dependency.
R(A, B, C) with A → B is split into R1(A, B) and R2(A, C). The split is —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The shared column A determines B, so it is a key of R1 and the join returns exactly the original rows.
A BCNF decomposition always preserves every functional dependency.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
A lossless BCNF decomposition can lose a dependency; a 3NF decomposition can always be both lossless and dependency-preserving.
Find the keys with closure
- The attribute closure 属性闭包 $X^+$ is everything that $X$ determines. For $R(A, B, C, D)$ with $A \to B$, $B \to C$, $C \to D$: $A^+ = \{A, B, C, D\}$, so $A$ alone is a key.
- $B^+ = \{B, C, D\}$ does not contain $A$, so $B$ is not a key. Closure turns "is this a key?" and "does this dependency follow?" into a mechanical check.
$R(A, B, C, D)$ with $A \to B$, $B \to C$, $C \to D$. $B^+$ is:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
$B \to C \to D$, but nothing gives $A$.
In that relation, $A$ is a key.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
$A^+$ contains every attribute.
Worked example: 3NF but not BCNF
Teach(student, course, instructor): each instructor teaches one course (instructor → course), and a student has one instructor per course (student, course → instructor).instructoris a determinant but not a key, so the table is not in BCNF. Splitting into(instructor, course)and(student, instructor)is lossless, but no single table now enforcesstudent, course → instructor. In this case 3NF is the better place to stop.
Teach(student, course, instructor), with exactly the dependencies instructor → course and student, course → instructor (and their consequences), is in BCNF.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Instructor determines course but is not a key.
Marks that slip away
- Splitting on a non-key. Split on
titleand two courses that share a title join into spurious rows. - Mixing up the two dependencies.
course → titlein the enrolment table depends on part of the key (a 2NF problem);title → dept → buildingis transitive (a 3NF problem). - Normalising past the evidence. Controlled denormalisation with a refresh rule is fine once measured reads need it.
Recap
- Anomalies come from repeated facts: insert, update and delete. Each normal form removes one cause:
| Form | Removes, with this lesson's case |
|---|---|
| 2NF | dependence on part of the key: course → title |
| 3NF | transitive dependence: title → dept → building |
| BCNF | a determinant that is not a key: instructor → course |
- Normalise to reason; denormalise when measurements show the need.
出了什么问题?只有网站团队能看到这些信息。