A3.2.5 · Database design
A3.2.5 · 数据库设计
The difference between normal forms
各个范式之间的区别
If fixing one instructor's typo means editing the same name in five different rows, the table is breaking a specific, nameable rule, and normal forms are how you name which one.
如果修正一位讲师的一个拼写错误,需要在五个不同的行里改同一个名字,说明这张表违反了某条具体的、可以指名的规则,而范式正是用来说出违反了哪一条。
Explainer
讲解
A workshop signup sheet lists every participant alongside the workshop they joined, its title, and the instructor teaching it, all crammed into one table. Rename the instructor, and that new name has to be typed into every single row they appear in. Miss one, and the table now silently disagrees with itself about who's teaching what. Normalization is the fix, and it comes in graded stages, each one closing off a specific way this kind of table can go wrong.
一张工作坊报名表,把每位参与者和他们加入的工作坊、工作坊的标题,以及授课讲师,全都挤在一张表里。给讲师改名,新名字就得在他出现的每一行里重新输入。漏掉一行,这张表就会悄悄地自相矛盾,对谁在教什么说法不一。规范化就是解决办法,它分成有等级的几个阶段,每个阶段都堵住这类表可能出错的一种具体方式。
Normalization is the discipline of restructuring a table's fields so every fact lives in exactly one place, rather than copied wherever it happens to be needed. Fewer duplicate copies also means a physically smaller table, and a smaller table simply has less for the disk to read on every query, so this pays off in raw speed too, not just tidiness.
规范化(normalization)是重新组织表中字段的方法,使每个事实只存在于一个地方,而不是在需要的每个地方都复制一份。重复的副本更少,也就意味着表的物理体积更小,而更小的表在每次查询时需要磁盘读取的内容就更少,所以这不只是整洁,在速度上也有回报。
What normalization is actually fixing
规范化究竟在修正什么
Real problems in an unnormalized table
未规范化的表中的真实问题
- Data duplication: the same fact (an instructor's name, a workshop's title) stored in many rows instead of one.
- 数据重复:同一个事实(讲师的名字、工作坊的标题)被存放在许多行里,而不是一处。
- Missing data: a fact can't be recorded at all until it happens to appear alongside something else, e.g. a new workshop with no participants yet has nowhere to live.
- 数据缺失:一个事实在恰好与其他东西同时出现之前,根本无法被记录,比如一个还没有任何参与者的新工作坊,就没有地方存放。
- Dependency concerns: a field that looks like it belongs to the row actually depends on something narrower than the whole key, or on a different field entirely. Several distinct flavours of this exist, covered below.
- 依赖问题:一个看起来属于这一行的字段,实际上依赖的是比整个键更窄的东西,或者完全是另一个字段。这有好几种不同的形式,下面会讲到。
The vocabulary normal forms are built from
构成范式的基本词汇
Try to split a field's content into two meaningful pieces, name and surname out of a full name, say, and if that split is possible, the field was never atomic to begin with: atomic means one cell, one fact, nothing hiding inside it that could be pulled apart further. Unique identification is the simpler of the two: some field, or some combination of fields, has to pin down one row and one row only, with zero ambiguity about which row is meant.
试着把一个字段的内容拆成两个有意义的部分,比如把全名拆成名和姓,如果这种拆分是可能的,那么这个字段一开始就不是原子的:原子的意思是一个单元格,一个事实,里面没有藏着任何还能再拆开的东西。唯一标识是两者中较简单的:某个字段或某几个字段的组合,必须能确定一行而且只确定一行,对指的是哪一行毫无歧义。
Whenever knowing one field's value pins down another field's value automatically, that second field is said to have a functional dependency on the first, the first is its determinant, the second is its dependent. The relationship only runs one way: the determinant fixes the dependent, but the dependent tells you nothing about the determinant. Three specific kinds matter here:
每当知道了一个字段的值就能自动确定另一个字段的值,就说第二个字段对第一个字段有函数依赖(functional dependency),第一个是它的决定因素,第二个是被决定的。这种关系只朝一个方向:决定因素确定被决定的字段,但被决定的字段并不能告诉你决定因素是什么。这里有三种具体的类型很重要:
Three kinds of dependency
三种依赖
- Full functional dependency: the dependent attribute relies on the WHOLE primary key. Healthy, this is what every non-key attribute should have.
- 完全函数依赖:被决定的属性依赖于整个主键。这是健康的,每个非键属性都应该是这样。
- Partial dependency: the primary key is composite, made of two or more fields together, and a non-key attribute leans on only one piece of that combination rather than needing the whole thing.
- 部分依赖:主键是复合的,由两个或更多字段共同组成,而某个非键属性只依赖这个组合中的一部分,而不是需要整个组合。
- Transitive dependency: a non-key attribute's real determinant is a DIFFERENT non-key attribute sitting in the same table, not the primary key at all, the primary key only reaches it secondhand.
- 传递依赖:某个非键属性真正的决定因素是同一张表中另一个非键属性,而完全不是主键,主键只是间接地到达它。
1NF, 2NF, 3NF: what each one requires, and what it fixes
1NF、2NF、3NF:各自的要求,以及各自修正什么
A working primary key exists, no attribute is repeated, no group of attributes recurs, and every field genuinely holds one indivisible value.
存在一个有效的主键,没有重复的属性,没有重复出现的属性组,并且每个字段确实只保存一个不可再分的值。
Fixes: combined, non-atomic fields and repeating groups.
修正:合并的、非原子的字段,以及重复组。
Example: see Step 2 of the stepper below.
例子:见下面步进器的第 2 步。
Everything 1NF already requires, PLUS every non-key field genuinely needs the complete primary key to determine it, not just a slice of it, so no partial dependency survives.
满足 1NF 已经要求的一切,并且每个非键字段确实需要完整的主键才能确定它,而不只是其中的一部分,所以不存在部分依赖。
Fixes: partial-key dependencies (only relevant when the key is composite).
修正:部分键依赖(只在键是复合键时才有意义)。
Example: see Step 3 of the stepper below.
例子:见下面步进器的第 3 步。
Everything 2NF already requires, PLUS tracing any non-key field back to its determinant leads straight to the primary key, with no detour through some other non-key field along the way.
满足 2NF 已经要求的一切,并且把任何非键字段追溯到它的决定因素,都直接指向主键,中途不绕经任何其他非键字段。
Fixes: transitive (non-key) dependencies.
修正:传递(非键)依赖。
Example: see Step 4 of the stepper below.
例子:见下面步进器的第 4 步。
Worked example
例题讲解
One unnormalized table, several real violations
一张未规范化的表,好几处真实的违规
WORKSHOP_SIGNUP tracks who's attending which workshop. Click any column to see exactly which rule it breaks, and why.
WORKSHOP_SIGNUP 记录谁参加了哪个工作坊。点击任一列,查看它具体违反了哪条规则,以及为什么。
| P01 | Noor Aziz | W1 | Digital Photography | I1 | Marcus Webb |
| P02 | Tariq Idris | W1 | Digital Photography | I1 | Marcus Webb |
| P01 | Noor Aziz | W2 | Pottery Basics | I2 | Sana Farouk |
Click a column header to see the rule it breaks (or satisfies).
点击列标题,查看它违反(或满足)的规则。
Click a column above to see its dependency status.
点击上面的一列,查看它的依赖状态。
Now watch it actually get fixed, one normal form at a time
现在看它是怎样一个范式一个范式地被真正修正的
Same table, same violations, click through and see exactly what each normal form changes.
同一张表,同样的违规,点击查看每个范式具体改变了什么。
Check your understanding
检查你的理解
InstructorName depends on InstructorID, which is itself just a non-key attribute in the table, not part of the primary key. What kind of dependency is this?
InstructorName 依赖于 InstructorID,而 InstructorID 本身只是这张表中的一个非键属性,不属于主键。这是哪种依赖?
Common mistake
常见错误
A3.2.5A3.2.5 的这一常见模式RecurringPartial dependency and transitive dependency get confused constantly, since both are "a non-key attribute depending on the wrong thing." The difference is WHAT the wrong thing is: partial dependency is about depending on only part of a composite primary key, transitive dependency is about depending on another non-key attribute instead of the primary key at all.
反复出现部分依赖和传递依赖总是被混淆,因为两者都是"非键属性依赖了不该依赖的东西"。区别在于这个"不该依赖的东西"是什么:部分依赖是指只依赖复合主键的一部分,传递依赖是指依赖另一个非键属性,而完全不是主键。
What a weak answer looks like: "InstructorName is a partial dependency because it doesn't depend on the whole key." Half right, it doesn't depend on the key, but not because of a composite-key split, InstructorName depends on a DIFFERENT non-key field entirely.
较弱的答案是这样的:"InstructorName 是部分依赖,因为它不依赖整个键。"对了一半,它确实不依赖键,但原因并不是复合键被拆分,InstructorName 依赖的是另一个非键字段。
What it should say: "InstructorName is a transitive dependency, it depends on InstructorID, a non-key attribute, rather than depending directly on the primary key." Names the correct dependency type by identifying exactly what it depends on.
应该这样写:"InstructorName 是传递依赖,它依赖的是 InstructorID 这个非键属性,而不是直接依赖主键。"通过指出它究竟依赖什么,点出了正确的依赖类型。
Revision questions
复习题
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
仿真题风格。先写出你的答案,再显示评分要点并给自己打分。
[2]Explain what is meant by atomicity, and give an example of a field that is not atomic.
[2]解释什么是原子性,并给出一个非原子字段的例子。
Marking points
评分要点
- Atomicity means every field holds a single indivisible value that cannot be meaningfully split further.
- Example of a non-atomic field: one PhoneNumbers field holding 020-111, 020-222 (or a Name field holding first and last together).
- 原子性指每个字段持有一个不可再分、无法进一步有意义拆分的单一值。
- 非原子字段的例子:一个 PhoneNumbers 字段同时存 020-111、020-222(或一个 Name 字段把名和姓存在一起)。
[3]Explain the difference between a partial dependency and a transitive dependency.
[3]解释部分依赖与传递依赖的区别。
Marking points
评分要点
- A partial dependency is when a non-key field depends on only part of a composite primary key.
- A transitive dependency is when a non-key field depends on another non-key field rather than on the key.
- Removing partial dependencies gives 2NF; removing transitive dependencies gives 3NF.
- 部分依赖是指一个非键字段只依赖于复合主键的一部分。
- 传递依赖是指一个非键字段依赖于另一个非键字段,而不是依赖于键。
- 去除部分依赖得到 2NF;去除传递依赖得到 3NF。
[4]A table ORDER_ITEM(OrderID, ProductID, ProductName, Quantity) uses a composite primary key of OrderID and ProductID. ProductName depends only on ProductID. Identify the type of dependency this creates, and explain which normal form it violates.
[4]表 ORDER_ITEM(OrderID, ProductID, ProductName, Quantity)使用 OrderID 和 ProductID 组成的复合主键。ProductName 只依赖 ProductID。指出这造成了哪种类型的依赖,并解释它违反了哪个范式。
Marking points
评分要点
- ProductName depends on ProductID, which is only part of the composite key (OrderID, ProductID): this is a partial dependency.
- A partial dependency violates second normal form (2NF).
- The fix is to move ProductID and ProductName into a separate PRODUCT table.
- ProductName 依赖于 ProductID,而 ProductID 只是复合键 (OrderID, ProductID) 的一部分:这是部分依赖。
- 部分依赖违反第二范式(2NF)。
- 修正方法是把 ProductID 和 ProductName 移到单独的 PRODUCT 表。
Flashcards
闪卡
Active recall for this statement's key terms.
针对本条陈述关键术语的主动回忆。
Keep this statement's practice with you
把本条陈述的练习带在身边
Free account: unlock every revision question and flashcard for A3.2.5, and every other IB CS 2027 statement.
免费账号:解锁 A3.2.5 以及其他所有 IB CS 2027 陈述的全部复习题和闪卡。