E-R diagrams and normalisation · E-R 图和规范化
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| entity-relationship diagram/ˈentɪti rɪˈleɪʃənʃɪp ˈdaɪəɡræm/ | 实体关系图 | shí tǐ guān xì tú |
| normalisation/ˌnɔːməlaɪˈzeɪʃn/ | 规范化 | guī fàn huà |
| entity/ˈentɪti/ | 实体 | shí tǐ |
| cardinality/ˌkɑːdɪˈnælɪti/ | 基数 | jī shù |
| one-to-many/wʌn tə ˈmeni/ | 一对多 | yī duì duō |
| many-to-many/ˈmeni tə ˈmeni/ | 多对多 | duō duì duō |
| link table/lɪŋk ˈteɪbl/ | 连接表 | lián jiē biǎo |
| normal forms/ˈnɔːml fɔːmz/ | 范式 | fàn shì |
| atomic/əˈtɒmɪk/ | 原子 | yuán zi |
| transitive dependency/ˈtrænsɪtɪv dɪˈpendənsi/ | 传递依赖 | chuán dì yī lài |
Forty rows to change one phone number
- A school keeps its enrolments in one spreadsheet. Every row is one student on one course, and every row also carries the student's form tutor and the tutor's phone number.
- The tutor changes her number. Forty rows have to be edited. Thirty-nine are. For the rest of the year, one course list rings a stranger.
- Nothing was mistyped. The fault was in the design: a fact that belongs to the tutor was stored once per enrolment, so it could be true in one place and false in another.
- This lesson is the two design tools that prevent that: the entity-relationship diagram 实体关系图, which draws the structure, and normalisation 规范化, which removes the repetition.
改一个电话号码要改四十行
- 一所学校把选课记录放在一张电子表格里。每一行是一个学生的一门课,每一行还带着该学生的班主任和班主任的电话号码。
- 班主任换了号码。四十行要改。改了三十九行。这一年余下的时间里,有一张课程名单打给一个陌生人。
- 没有任何东西被打错。问题在设计:属于班主任的一个事实,每条选课记录存了一次,所以它可以在一处为真、在另一处为假。
- 这一课讲防止这种情况的两种设计工具:画出结构的实体关系图(entity-relationship diagram),和消除重复的规范化(normalisation)。
Entity-relationship diagrams
- An entity-relationship diagram (E-R diagram) documents a database design: each entity 实体 is a rectangle, each relationship is a line between two rectangles, and the cardinality 基数 is marked at each end.
- In crow's-foot notation a single bar means "one" and a three-pronged foot means "many". Read each line in both directions: each customer places many orders; each order is placed by one customer.
- The diagram is drawn before any table is created, and the relationships on it become the foreign keys.
One rectangle per entity, one line per relationship
A bar for one, a foot for many
实体关系图
- 实体关系图(E-R 图)记录数据库设计:每个实体(entity)是一个矩形,每个关系是两个矩形之间的一条线,基数(cardinality)标在每一端。
- 鸦爪记法中,一条竖线表示"一",三叉的爪表示"多"。每条线要往两个方向读:每个客户下多笔订单;每笔订单由一个客户下。
- 图在创建任何表之前画出,图上的关系变成外键。

每个实体一个矩形,每个关系一条线

竖线表示一,爪表示多
In an E-R diagram, the number of one entity that can relate to one of the other, marked at each end of the line, is the ____. · 在 E-R 图中,一个实体可以与另一个实体的多少个实体相关联的数量,标记在线条的两端,称为 ____。
Cardinality is one or many at each end; crow's-foot notation draws a bar for one and a foot for many. · 基数在每一端是一个或多个;乌鸦脚符号用一条竖线表示一个,用脚表示多个。
The three kinds of relationship
- One-to-one (1:1): each member has one library card and each card belongs to one member. Rare; the two entities are often merged into one table.
- One-to-many 一对多 (1:M): one customer places many orders; each order belongs to one customer. Implemented by putting the "one" side's primary key into the "many" side's table as a foreign key.
- Many-to-many 多对多 (M:N): a student takes many courses and a course has many students. It cannot be implemented directly; it needs a link table.
三种关系
- 一对一(1:1):每个会员有一张借书卡,每张卡属于一个会员。少见;两个实体常合并成一张表。
- 一对多(one-to-many,1:M):一个客户下多笔订单;每笔订单属于一个客户。实现方法是把"一"方的主键作为外键放进"多"方的表。
- 多对多(many-to-many,M:N):一个学生修多门课,一门课有多个学生。它无法直接实现;需要连接表。
Each customer can place many orders, but each order belongs to one customer. This relationship is: · 每个客户可以下很多订单,但每个订单只属于一个客户。这种关系是:
One customer → many orders, each order → one customer: a one-to-many relationship. · 一个客户 → 多个订单,每个订单 → 一个客户:这是一对多关系。
Match each relationship to its cardinality. · 将每种关系与其基数匹配。
1:1 each side has one; 1:M one side has many; M:N both sides have many (needs a link table). · 1:1 每边各有一个;1:M 一边有多个;M:N 两边都有多个(需要链接表)。
Worked example: draw the E-R diagram
- A school has teachers, classes and students. Each teacher teaches many classes; each class is taught by one teacher. Each class has many students; each student is in one class. Students may join many clubs and each club has many students.
- Four rectangles:
TEACHER,CLASS,STUDENT,CLUB.TEACHER—CLASSis one-to-many, the foot atCLASS.CLASS—STUDENTis one-to-many, the foot atSTUDENT.STUDENT—CLUBis many-to-many, a foot at both ends. - The marks: every entity present, every relationship drawn, and the correct cardinality symbol at each end. A line with no symbols is half an answer.
例题:画出 E-R 图
- 一所学校有教师、班级和学生。每位教师教多个班;每个班由一位教师教。每个班有多个学生;每个学生在一个班。学生可以加入多个社团,每个社团有多个学生。
- 四个矩形:
TEACHER、CLASS、STUDENT、CLUB。TEACHER—CLASS一对多,爪在CLASS。CLASS—STUDENT一对多,爪在STUDENT。STUDENT—CLUB多对多,两端都是爪。 - 得分点:每个实体都在,每个关系都画出,每一端都有正确的基数符号。没有符号的线只是半个答案。
Link tables
- A many-to-many relationship is broken into two one-to-many relationships through a link table 连接表 that holds the two foreign keys.
ENROLMENT(StudentID, CourseID, EnrolmentDate): one student has many enrolments, one course has many enrolments, and each row is one student on one course. Its primary key is the composite of the two foreign keys.- Data about the pairing itself, the date, a grade, goes in the link table; data about the student or the course stays in its own table.
One many-to-many becomes two one-to-many
连接表
- 多对多关系通过一张保存两个外键的连接表(link table)被拆成两个一对多关系。
ENROLMENT(StudentID, CourseID, EnrolmentDate):一个学生有多条选课记录,一门课有多条选课记录,每一行是一个学生的一门课。它的主键是两个外键的复合。- 关于这个配对本身的数据——日期、成绩——放在连接表里;关于学生或课程的数据留在各自的表里。

一个多对多变成两个一对多
How is a many-to-many relationship implemented in a relational database? · 如何在关系数据库中实现多对多关系?
A link (junction) table holds a foreign key to each side, turning M:N into two 1:M relationships. · 链接(连接)表保存指向每一侧的外键,将 M:N 转换为两个 1:M 关系。
ENROLMENT(StudentID, CourseID, EnrolmentDate) is a link table. Which statements are true? Select all · 所有 that apply. · ENROLMENT(StudentID, CourseID, EnrolmentDate) 是一个链接表。哪些陈述是正确的?选择所有适用项。
The link table holds the pairing and facts about the pairing. The student's own data stays in STUDENT, or it would repeat on every enrolment. · 链接表保存配对以及关于配对的详细信息。学生自己的数据保留在 STUDENT 中,否则每次注册都会重复。
Normalisation
- Normalisation organises the tables so that each fact is stored exactly once, cutting redundancy and inconsistency. It passes through the normal forms 范式 in order: first, second, third.
- The procedure: find the entities and their attributes; choose a primary key for each; remove repeating groups and non-atomic values (1NF); remove attributes that depend on only part of a composite key (2NF); remove attributes that depend on another non-key attribute (3NF); add foreign keys for the relationships.
- The cost is more tables and more joins. The exam asks for 3NF.
规范化
- 规范化组织表,使每个事实恰好存一次,减少冗余和不一致。它按顺序经过各范式(normal forms):第一、第二、第三。
- 步骤:找出实体及其属性;为每个选一个主键;去掉重复组和非原子值(1NF);去掉只依赖复合键一部分的属性(2NF);去掉依赖另一个非键属性的属性(3NF);为关系添加外键。
- 代价是更多的表和更多的连接。考试要求 3NF。
Database service lab · 数据库服务实验
Watch how a DBMS turns a query into safe shared data access. · 观看 DBMS 如何将查询转化为安全的共享数据访问。
Put the normal forms in the order you apply them. · 按你应用的顺序排列范式。
You reach 3NF by passing through 1NF then 2NF — each builds on the previous. · 你到达 3通过 1然后经过 2——每个都以前一个为基础。
The main aim of normalisation is to: · 规范化的主要目的是:
Normalising to 3NF stores each fact once, removing update/insert/delete anomalies (at the cost of more joins). · 规范化到 3NF 时,每个事实只存储一次,从而消除更新/插入/删除异常(代价是需要更多的连接操作)。
First normal form
- A table is in 1NF when every field holds a single, atomic 原子 value, there are no repeating groups, and there is a primary key.
STUDENT(StudentID, Name, Phone)withPhoneholding0123, 0456is not atomic.STUDENT(StudentID, Name, Course1, Course2, Course3)has a repeating group.- Fix both by moving the repeated data to its own table with a row per value:
STUDENT_PHONE(StudentID, Phone),ENROLMENT(StudentID, CourseID).
第一范式
- 当每个字段只保存一个原子(atomic)值、没有重复组、并且有主键时,表处于 1NF。
STUDENT(StudentID, Name, Phone)中Phone保存0123, 0456就不是原子的。STUDENT(StudentID, Name, Course1, Course2, Course3)有重复组。- 两者都通过把重复数据移到自己的表、每个值一行来修正:
STUDENT_PHONE(StudentID, Phone)、ENROLMENT(StudentID, CourseID)。
A Phone field holds "0123, 0456" for one student. Which normal form does the table fail? · Phone 字段为一名学生保存了 "0123, 0456"。该表违反了哪种范式?
Two values in one cell is the 1NF failure. Move the numbers to STUDENT_PHONE(StudentID, Phone), one per row. · 一个单元格中有两个值是 1NF 失败。将数字移动到 STUDENT_PHONE(StudentID, Phone),每行一个。
Worked example: second normal form
ORDER(OrderID, CustomerID, CustomerName, ProductID, Quantity)has the composite primary key(OrderID, ProductID). Is it in 2NF?- Test each non-key field against the whole key.
Quantitydepends on bothOrderIDandProductID: which order, which product. Fine. CustomerIDandCustomerNamedepend onOrderIDalone, only part of the key: a partial dependency, so the table is not in 2NF. Split it:ORDER(OrderID, CustomerID, CustomerName)andORDER_LINE(OrderID, ProductID, Quantity).
例题:第二范式
ORDER(OrderID, CustomerID, CustomerName, ProductID, Quantity)的复合主键是(OrderID, ProductID)。它在 2NF 吗?- 把每个非键字段对整个键测试。
Quantity同时依赖OrderID和ProductID:哪笔订单、哪个商品。没问题。 CustomerID和CustomerName只依赖OrderID,即键的一部分:部分依赖,所以表不在 2NF。拆分:ORDER(OrderID, CustomerID, CustomerName)和ORDER_LINE(OrderID, ProductID, Quantity)。
Worked example: third normal form
- Is
ORDER(OrderID, CustomerID, CustomerName)in 3NF? - A table is in 3NF when it is in 2NF and every non-key field depends only on the primary key, not on another non-key field.
CustomerNamedepends onCustomerID, which is not the key: a transitive dependency 传递依赖, so the table is not in 3NF. - Split again:
ORDER(OrderID, CustomerID)andCUSTOMER(CustomerID, CustomerName), withCustomerIDa foreign key. The name is now stored once, however many orders the customer places.
Each form removes one kind of dependency
例题:第三范式
ORDER(OrderID, CustomerID, CustomerName)在 3NF 吗?- 当表在 2NF 且每个非键字段只依赖主键、不依赖另一个非键字段时,表处于 3NF。
CustomerName依赖不是键的CustomerID:传递依赖(transitive dependency),所以表不在 3NF。 - 再拆:
ORDER(OrderID, CustomerID)和CUSTOMER(CustomerID, CustomerName),CustomerID是外键。无论客户下多少订单,姓名现在只存一次。

每个范式消除一种依赖
Normalising to 3NF stores each fact once and removes update anomalies, at the cost of more tables and joins. · 规范化到 3NF 时,每个事实只存储一次并消除更新异常,代价是增加了表和连接操作。
That trade-off — cleaner data versus more joins — is why 3NF is the usual target. · 这种权衡——更干净的数据与更多的连接操作——正是 3NF 成为通常目标的原因。
Saying why a table is, or is not, in 3NF
- Not in 3NF: name the dependency. "
TEACHERis not in 3NF because the non-key attributeDepartmentNamedepends on the non-key attributeDepartmentID, not on the primary key." - In 3NF: cover all three conditions. "Every attribute is atomic with no repeating groups; there is no partial dependency on part of the key; every non-key attribute depends only on the primary key, with no transitive dependency."
- Then, if asked, give the normalised tables in the standard notation with the foreign keys marked.
说明表为什么在或不在 3NF
- 不在 3NF:说出依赖。"
TEACHER不在 3NF,因为非键属性DepartmentName依赖非键属性DepartmentID,而不是主键。" - 在 3NF:覆盖三个条件。"每个属性都是原子的,没有重复组;不存在对键的一部分的部分依赖;每个非键属性只依赖主键,没有传递依赖。"
- 然后,若被要求,用标准记法给出规范化后的表并标出外键。
In TEACHER(TeacherID, Name, DepartmentID, DepartmentName), why is the table not in 3NF? · 在 TEACHER(TeacherID, Name, DepartmentID, DepartmentName) 中,为什么该表不在 3NF?
A transitive dependency. Split out DEPARTMENT(DepartmentID, DepartmentName) and keep DepartmentID in TEACHER as a foreign key. · 这是一个传递依赖。拆分出 DEPARTMENT(DepartmentID, DepartmentName),并在 TEACHER 中保留 DepartmentID 作为外键。
Marks that slip away
- 2NF is only a question when the primary key is composite. A single-field key cannot have a partial dependency.
- 3NF requires 2NF. Say both when justifying.
- Atomic means one value per cell. "Two phone numbers in one field" is a 1NF failure, not a 3NF one.
- Naming the normal form is not the answer; naming the dependency is. More tables after normalising is the point, not a fault.
容易丢掉的分
- 只有主键是复合的时候 2NF 才成为问题。单字段键不可能有部分依赖。
- 3NF 要求 2NF。论证时两者都要说。
- 原子意味着每个单元一个值。"一个字段里两个电话号码"是 1NF 的失败,不是 3NF 的。
- 说出范式的名字不是答案;说出依赖才是。规范化后表更多是目的,不是缺点。
You've got it
- an E-R diagram shows entities as rectangles and relationships as lines with the cardinality at each end: 1:1, 1:M, M:N
- a many-to-many relationship is stored through a link table of the two foreign keys, its primary key their composite
- 1NF atomic values, no repeating groups, a primary key · 2NF no partial dependency on part of a composite key · 3NF no transitive dependency between non-key attributes
- justify by naming the dependency, then give the split tables with their foreign keys
你掌握了
- E-R 图把实体画成矩形、关系画成两端带基数的线:1:1、1:M、M:N
- 多对多关系通过由两个外键组成的连接表存储,其主键是两者的复合
- 1NF 原子值、无重复组、有主键 · 2NF 对复合键的一部分没有部分依赖 · 3NF 非键属性之间没有传递依赖
- 论证时说出依赖,再给出带外键的拆分后的表