First exams 2027 · SL & HL2027 年首次开考 · SL 与 HL

A3.2.2 · Database design

A3.2.2 · 数据库设计

Constructing entity-relationship diagrams

构建实体-关系图(ERD)

Before a single table gets built, a good designer sketches it, an ERD is that sketch, made precise enough that two different people would build the exact same database from it.

在建任何一张表之前,好的设计者会先画出草图,ERD 就是这张草图,只是精确到让两个不同的人依据它能建出完全相同的数据库。

Core for SL & HL Command term: construct Builds on A3.1.1 & A3.2.1
SL 与 HL 核心内容 指令词:construct 建立在 A3.1.1 与 A3.2.1 之上

Explainer

讲解

Eastgate Food Bank wants a system to manage its volunteer rota. Before anyone writes a single line of SQL, the designer sketches it out: a box for VOLUNTEER, a box for SHIFT, a line between them. But "a line between them" isn't enough information to build a real database from, the designer also has to answer two separate questions: how many shifts can one volunteer sign up for, and does a shift actually need at least one volunteer assigned before it can run? An ERD answers both, precisely, using notation instead of prose.

Eastgate 食物银行想要一个管理志愿者排班的系统。在任何人写下一行 SQL 之前,设计者先画出草图:一个 VOLUNTEER 方框,一个 SHIFT 方框,中间一条线。但"中间一条线"的信息还不足以建出真正的数据库,设计者还必须回答两个独立的问题:一位志愿者可以报名几个班次?一个班次是否至少要分配一位志愿者才能开始?ERD 用符号而不是文字,精确地回答这两个问题。

This is A3's guiding question in a very concrete form: what are the principles, structures and operations that form the basis of database systems? A3.2.1 described schemas in words; an ERD is how the conceptual schema's structure gets drawn, and it's the standard way stakeholders, designers and future developers all read the same design without arguing over prose.

这是 A3 的引导性问题的一个非常具体的形式:构成数据库系统基础的原理、结构和操作是什么?A3.2.1 用文字描述了模式;ERD 则是把概念模式的结构画出来的方式,也是利益相关者、设计者和未来的开发者阅读同一份设计、而不必为文字措辞争论的标准方法。

Df

An entity-relationship diagram (ERD) draws the entities a database holds as boxes, and draws how those entities connect to each other as lines between the boxes, turning a design that would otherwise live only in someone's head into something a whole team can actually look at together. That shared picture keeps paying off long after the first design meeting: it is what a new developer reads to understand the schema months later, what a database constraint gets checked against before it is added, and the reference everyone returns to once the "why did we build it this way" question inevitably comes up.

实体-关系图(ERD)把数据库保存的实体画成方框,把这些实体之间的联系画成方框之间的连线,让原本只存在于某个人脑子里的设计,变成整个团队可以一起查看的东西。这张共同的图在第一次设计会议之后很久仍然有用:新开发者几个月后靠它理解模式,添加数据库约束前要对照它检查,而当"我们为什么这样设计"的问题不可避免地出现时,大家也都会回头参考它。

Two separate questions, two separate axes

两个独立的问题,两个独立的维度

Every relationship line on an ERD is really answering two independent questions about each side of it, and mixing them up is the single most common way to misread or mis-draw one.

ERD 上的每一条关系线,其实都在对它的每一侧回答两个相互独立的问题,而把它们混为一谈,是读错或画错 ERD 最常见的原因。

?

Cardinality answers "how many, at most?" Fix a single record on one side of the relationship, then count how many records on the other side it is allowed to reach. That upper limit is the cardinality, and it gives the four familiar shapes: one-to-one, one-to-many, many-to-one, or many-to-many.

基数(cardinality)回答"最多有多少个?"固定关系一侧的一条记录,数一数它最多可以对应另一侧的多少条记录。这个上限就是基数,它给出四种熟悉的形态:一对一、一对多、多对一,或多对多。

!

Modality asks something cardinality never touches: does this side of the relationship have to be filled in at all? Zero means it can genuinely be left empty, one means every instance on the other side needs at least one match here. Because the two questions (how many, at minimum how many) are answered completely separately, all four combinations exist in practice: mandatory-and-one, mandatory-and-many, optional-and-one, optional-and-many.

参与性(modality)问的是基数从不涉及的东西:关系的这一侧是否必须填上?零表示它确实可以留空,一表示另一侧的每个实例在这里至少需要一个匹配。因为这两个问题(最多多少个、至少多少个)是完全分开回答的,所以四种组合在实践中都存在:必选且一个、必选且多个、可选且一个、可选且多个。

A reusable technique: the "I am..." rule

一个可复用的技巧:"我是……"规则

Both axes have to be checked from BOTH entities' points of view separately, and the easiest way to do that reliably is to mentally become each entity in turn and ask two fixed questions.

两个维度都必须分别从两个实体各自的角度检查,而要可靠地做到这一点,最简单的办法是依次在心里"变成"每个实体,问两个固定的问题。

Pretend you ARE one of the two entities, looking across at the other one, then ask:

假装你就是这两个实体之一,望向另一个实体,然后问:

Cardinality (max)基数(最大)

"Can I have more than one of the other thing?" Yes → many. No → one.

"我可以拥有不止一个对面的东西吗?"是 → 多。否 → 一。

Modality (min)参与性(最小)

"Do I NEED the other thing just to exist?" Yes → mandatory (1). No → optional (0).

"我是否需要对面的东西才能存在?"是 → 必选(1)。否 → 可选(0)。

Then swap: become the OTHER entity and ask the exact same two questions again. Each entity answers for itself, about the entity across from it, never about itself.

然后调换:变成另一个实体,再把完全相同的两个问题问一遍。每个实体为自己回答,说的是对面的那个实体,而不是它自己。

Reading the notation: modality × cardinality

读懂符号:参与性 × 基数

The crow's-foot symbols aren't six unrelated shapes to memorise, four of them are just modality and cardinality drawn together on the same end of a line. The tick and circle answer the modality question; the single line and crow's foot answer the cardinality question.

鸡爪符号不是需要死记的六种互不相关的形状,其中四种只是参与性和基数画在线的同一端。竖杠和圆圈回答参与性的问题;单线和鸡爪回答基数的问题。

Cardinality: one
基数:一
Cardinality: many
基数:多
Modality: mandatory
参与性:必选
One and only one
一且仅一
Two ticks: must exist, and there is exactly one.
两道竖杠:必须存在,而且恰好一个。
One or many
一或多
Tick plus crow's foot: at least one must exist, more are allowed.
竖杠加鸡爪:至少必须存在一个,允许更多。
Modality: optional
参与性:可选
Zero or one
零或一
Circle plus tick: might not exist at all, but never more than one.
圆圈加竖杠:可能完全不存在,但绝不会多于一个。
Zero or many
零或多
Circle plus crow's foot: fully optional, with no upper limit either.
圆圈加鸡爪:完全可选,而且也没有上限。

Two real examples show why you can't guess modality from cardinality, or the other way round, they're genuinely independent:

两个真实的例子说明,你不能从基数猜出参与性,反过来也不行,它们确实是相互独立的:

Patient → medical recordsPatient → 病历

Most patients have medical records, but a newborn or brand-new patient might have none yet.

大多数病人都有病历,但新生儿或刚来的新病人可能还没有。

Zero or many: optional (a patient can have zero records) AND unlimited (can have many over time).

零或多:可选(病人可以有零份病历)而且没有上限(随时间推移可以有很多份)。

Order → customerOrder → 客户

Flip the logic around and it becomes obvious: nothing in the system called an "order" can ever exist unless some customer generated it by placing one.

把逻辑倒过来就一目了然:系统里任何叫做"订单"的东西,除非有某位客户下单产生了它,否则绝不可能存在。

One and only one: mandatory (an order must have a customer) AND exactly one (not several).

一且仅一:必选(订单必须有客户)而且恰好一个(不是好几个)。

Worked example

例题讲解

Constructing an ERD: a volunteer rota

构建 ERD:志愿者排班

VOLUNTEER and SHIFT are the two entities. Click through the real construction sequence, from raw entities to a finished, correctly notated relationship line.

VOLUNTEER 和 SHIFT 是两个实体。一步步点开真实的构建流程,从最初的实体,到一条完成的、符号正确的关系线。

Step 1 of 5 Entities

Applying the "I am..." rule to a new pair: Employee and Department

把"我是……"规则应用到新的一对:Employee 与 Department

Same technique, a fresh scenario this time, EMPLOYEE and DEPARTMENT in a company database, to prove the rule transfers rather than only working for VOLUNTEER and SHIFT.

同样的技巧,这次换一个新情境,公司数据库中的 EMPLOYEE 和 DEPARTMENT,以证明这条规则可以迁移,而不只是适用于 VOLUNTEER 和 SHIFT。

Direction 1: become the Employee, look at Department方向 1:变成 Employee,望向 Department

"I am an Employee. Can I belong to more than one department at once? No, just the one I'm assigned to. Do I NEED a department to exist as an employee record? Yes, every employee is assigned to a department."

"我是一名 Employee。我可以同时属于不止一个部门吗?不能,只属于分配给我的那一个。作为一条员工记录,我是否需要有一个部门才能存在?是的,每位员工都被分配到一个部门。"

Result: cardinality one, modality mandatory → one and only one (1, 1)

结果:基数为一,参与性为必选 → 一且仅一(1, 1)

Direction 2: become the Department, look at Employee方向 2:变成 Department,望向 Employee

"I am a Department. Can I have more than one employee? Yes, most departments have several. Do I NEED an employee to exist? No, a brand-new department might not have anyone assigned to it yet."

"我是一个 Department。我可以有不止一名员工吗?可以,大多数部门有好几名。我是否需要有员工才能存在?不需要,一个新成立的部门可能还没有分配任何人。"

Result: cardinality many, modality optional → optional many (0, N)

结果:基数为多,参与性为可选 → 可选的多(0, N)

Final answer: one department optionally has many employees (0, N); every employee mandatorily belongs to exactly one department (1, 1), a one-to-many relationship overall, from Department to Employee.

最终答案:一个部门可选地拥有多名员工(0, N);每名员工必须恰好属于一个部门(1, 1),整体上是从 Department 到 Employee 的一对多关系。

DEPARTMENT
mandatory + one
必选 + 一
EMPLOYEE
optional + many
可选 + 多

Read each symbol as describing the entity FARTHEST from it: the double-tick sits near DEPARTMENT but describes how many departments an employee needs (exactly one, mandatory), the circle-and-crow's-foot sits near EMPLOYEE but describes how many employees a department can have (zero to many, optional). This is the same reading direction used in the SHIFT/VOLUNTEER example above.

读每个符号时,要把它理解为描述离它最远的那个实体:双竖杠虽然靠近 DEPARTMENT,描述的却是一名员工需要多少个部门(恰好一个,必选);圆圈加鸡爪虽然靠近 EMPLOYEE,描述的却是一个部门可以有多少名员工(零到多个,可选)。这与上面 SHIFT/VOLUNTEER 例子中使用的读法方向相同。

Check your understanding

检查你的理解

"A shift must have at least one volunteer assigned." Is this statement describing cardinality or modality?

"一个班次必须至少分配一位志愿者。"这句话描述的是基数还是参与性?

Common mistake

常见错误

Markers reported this pattern for A3.2.2
阅卷员反馈了 A3.2.2 的这一常见模式

RecurringA relationship's modeling is only complete once BOTH cardinality and modality are pinned down, and candidates routinely skip that check, assuming one setting from the other instead of testing it separately. Calling a "many" relationship automatically "mandatory," or an "optional" one automatically "one," is the exact shape of that mistake. They're independent settings, a relationship can be optional-and-many just as easily as mandatory-and-one.

反复出现只有当基数和参与性都被确定下来,一个关系的建模才算完整,而考生常常跳过这一步检查,凭一个设置去推断另一个,而不是分别测试。把"多"的关系自动当作"必选",或把"可选"的关系自动当作"一",正是这种错误的典型形态。它们是相互独立的设置,一个关系完全可以是可选且多,就像它可以是必选且一那样。

✕

What a weak answer looks like: "Since a shift can have many volunteers, the relationship is optional." This confuses cardinality (many, a maximum) with modality (optional, a minimum), they were never linked.

较弱的答案是这样的:"因为一个班次可以有很多志愿者,所以这个关系是可选的。"这把基数(多,一个最大值)和参与性(可选,一个最小值)混为一谈,它们从来没有关联。

✓

What it should say: "A shift's cardinality with volunteers is many (a shift can have several volunteers rostered). Separately, a shift's modality is mandatory (a shift cannot run without at least one volunteer)." States each axis on its own terms, then combines them.

应该这样写:"班次与志愿者的基数是多(一个班次可以排上好几位志愿者)。另外,班次的参与性是必选(班次没有至少一位志愿者就无法开始)。"分别按各自的意思陈述每个维度,然后把它们合起来。

Revision questions

复习题

Past-paper style. Write an answer, then reveal the marking points and mark yourself.

仿真题风格。先写出你的答案,再显示评分要点并给自己打分。

Question 1 · 2 marks第 1 题 · 2 分

[2]Distinguish between cardinality and modality in an ERD.

[2]区分 ERD 中的基数和参与性。

Question 2 · 3 marks第 2 题 · 3 分

[3]A library system has BOOK and MEMBER entities, where a member can borrow many books, but a book that isn't currently borrowed has no associated member. State the cardinality and modality of this relationship on the MEMBER side, with a reason for each.

[3]一个图书馆系统有 BOOK 和 MEMBER 实体,一位会员可以借多本书,但一本当前没有被借出的书没有关联的会员。在 MEMBER 一侧,说明这个关系的基数和参与性,并各给出理由。

Question 3 · 4 marks第 3 题 · 4 分

[4]Construct an ERD, with correct crow's-foot notation, for a hospital system where a DOCTOR entity treats many PATIENT entities, and every patient must be assigned to exactly one doctor.

[4]为一个医院系统构建一个 ERD,使用正确的鸡爪符号,其中一个 DOCTOR 实体治疗多个 PATIENT 实体,并且每位病人必须被分配给恰好一位医生。

Flashcards

闪卡

Active recall for this statement's key terms.

针对本条陈述关键术语的主动回忆。

Prototype render. Concepts grounded in the IB CS 2027 syllabus for A3.2.2 (ERD definition and significance, cardinality and modality); worked examples are original. Not yet wired to live routes.