Normalisation
Đã giới thiệu
6:35
Cơ sở dữ liệu
A database is a set of true sentences about a domain. This course shows how to model, query, protect and govern data. The first three units model and shape it…
Thuyết minh tiếng Anh · Có thể chuyển đổi phụ đề tiếng Anh và tiếng Trung
Trong bài học này
| English | Ý nghĩa |
|---|---|
| insert anomaly/ˈɪnsɜːt əˈnɒməli/ | lỗi chèn |
| update anomaly/ˈʌpdeɪt əˈnɒməli/ | lỗi cập nhật |
| delete anomaly/dɪˈliːt əˈnɒməli/ | lỗi xóa |
| functional dependency/ˈfʌŋkʃənl dɪˈpendənsi/ | phụ thuộc hàm |
| Second normal form/ˈsekənd ˈnɔːml fɔːm/ | no non-key column depends on only part of a composite key |
| third normal form/θɜːd ˈnɔːml fɔːm/ | Dạng chuẩn thứ ba |
| Boyce-Codd normal form/bɔɪs kɒd ˈnɔːml fɔːm/ | every determinant in the table is a candidate key |
| denormalisation/dɪˌnɔːməlaɪˈzeɪʃn/ | deliberately adding redundancy to a schema to make reads faster |
| lossless/ˈlɒsləs/ | Describing compression from which the original data can be recovered exactly. |
| spurious rows/ˈspjʊərɪəs rəʊz/ | wrong extra rows created when a table is split and joined back badly |
| attribute closure/ˈætrɪbjuːt ˈkləʊʒə/ | the set of all attributes that a given set of attributes determines through the functional dependencies |
| transitive dependence |
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 —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
2NF removes dependencies on part of the key; 3NF the transitive ones.
Put the steps of normalising a table in order.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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 —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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) và R2(A, C). The split is —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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:
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$B \to C \to D$, but nothing gives $A$.
In that relation, $A$ is a key.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$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.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
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.
Đã xảy ra vấn đề gì? Chỉ đội ngũ trang web nhìn thấy thông tin này.