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

A3.2.6 · Database design

A3.2.6 · 数据库设计

Normalizing a database to 3NF

把数据库规范化到 3NF

A3.2.4 taught the key choices, A3.2.5 taught the dependency vocabulary, this statement is where both get used together to actually build a normalized database, for real, from a messy starting table.

A3.2.4 教了键的选择,A3.2.5 教了依赖的词汇,这条陈述则是把两者一起用起来,从一张杂乱的起始表出发,真正构建出一个规范化的数据库。

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

Explainer

讲解

A warehouse spreadsheet lists every stock order with the supplier's name and phone number copied onto every single line for that supplier. Change the supplier's phone number, and it has to change everywhere at once, or the database quietly starts disagreeing with itself. Normalizing to 3NF is the actual construction process that prevents this, not just a definition to recite, a repeatable procedure applied to a real starting table.

一张仓库电子表格列出每一笔进货订单,并把供应商的名称和电话号码复制到该供应商的每一行上。一旦供应商的电话号码变了,就必须同时在所有地方修改,否则数据库就会悄悄地自相矛盾。规范化到 3NF 就是防止这种情况的实际构建过程,不只是一个需要背诵的定义,而是应用在真实起始表上的一套可重复的步骤。

Df

Constructing a database normalized to 3NF means starting from an unnormalized table (or a described real-world scenario) and applying each normal form's requirement in order, splitting off attributes into new tables whenever a dependency rule is broken, until every remaining table satisfies 1NF, 2NF and 3NF at once.

构建一个规范化到 3NF 的数据库,意思是从一张未规范化的表(或一个被描述出来的真实情境)出发,依次应用每个范式的要求,每当违反某条依赖规则,就把相应的属性拆到新表里,直到剩下的每张表同时满足 1NF、2NF 和 3NF。

The procedure, in order

按顺序的步骤

1

Reach 1NF: give the table a primary key, make every field atomic, and move any repeating group of values into its own linked table.

达到 1NF:给表一个主键,让每个字段都是原子的,并把任何重复出现的值组移到它自己的关联表中。

2

Reach 2NF: find any non-key attribute that depends on only PART of a composite primary key, and move it into a new table keyed by that part.

达到 2NF:找出任何只依赖复合主键一部分的非键属性,把它移到一张以那一部分为键的新表中。

3

Reach 3NF: pick out any non-key attribute whose real determinant is a different non-key attribute rather than the key itself, and give it its own table, linked back through the attribute it actually depends on.

达到 3NF:找出任何真正的决定因素是另一个非键属性、而不是键本身的非键属性,给它建一张自己的表,并通过它实际依赖的那个属性链接回来。

This exact procedure works the same way whatever the real-world scenario, a library system, a hospital, an e-commerce platform, a school, an employee register, an inventory system, a police reporting system, only the entities and fields change. Two different scenarios are worked through fully below.

无论是什么现实情境,这个流程的做法都完全一样:图书馆系统、医院、电商平台、学校、员工名册、库存系统、警方报案系统,变的只有实体和字段。下面完整演示了两个不同的情境。

Worked example 1

例题 1

An inventory system, normalized step by step

一个库存系统,逐步规范化

STOCK_ORDER tracks what was ordered, from whom, and in what quantity. Click through and watch it get split apart, one violation at a time.

STOCK_ORDER 记录订了什么、向谁订的、订了多少。点击查看它如何被一个违规接一个违规地拆开。

Step 1 of 5 Unnormalized

Worked example 2

例题 2

The same procedure, a police crime-reporting system

同样的流程,一个警方报案系统

Different domain entirely, same three-step procedure. CRIME_REPORT logs which officers were assigned to which incident.

完全不同的领域,同样的三步流程。CRIME_REPORT 记录哪些警员被分配到哪起案件。

Unnormalized: CRIME_REPORT
未规范化:CRIME_REPORT
ReportIDOfficerIDOfficerNameStationIDStationNameCrimeTypeCrimeDate
R101O5Olivia HughesST2Riverside StationBurglary2027-01-14
R101O8Malik OseiST3Central StationBurglary2027-01-14
R102O5Olivia HughesST2Riverside StationVandalism2027-02-02

Primary key: (ReportID, OfficerID), since one report can involve several officers, and one officer works several reports. CrimeType and CrimeDate depend only on ReportID (partial dependency), OfficerName depends only on OfficerID (partial dependency, the OTHER direction on the same composite key), and StationName depends on StationID, which itself depends on OfficerID, not the primary key (transitive dependency).

主键:(ReportID, OfficerID),因为一份报告可以涉及好几位警员,而一位警员也处理好几份报告。CrimeType 和 CrimeDate 只依赖 ReportID(部分依赖),OfficerName 只依赖 OfficerID(部分依赖,是同一个复合键上另一个方向),而 StationName 依赖 StationID,StationID 又依赖 OfficerID,而不是主键(传递依赖)。

Normalized to 3NF: four tables
规范化到 3NF:四张表
ReportIDCrimeTypeCrimeDate
R101Burglary2027-01-14
R102Vandalism2027-02-02

REPORT: fixes the partial dependency on ReportID.

REPORT:修正对 ReportID 的部分依赖。

ReportIDOfficerID
R101O5
R101O8
R102O5

REPORT_OFFICER: the linking table for the many-to-many.

REPORT_OFFICER:用于多对多的连接表。

OfficerIDOfficerNameStationID
O5Olivia HughesST2
O8Malik OseiST3

OFFICER: fixes the partial dependency on OfficerID, StationID stays as a foreign key.

OFFICER:修正对 OfficerID 的部分依赖,StationID 仍作为外键保留。

StationIDStationName
ST2Riverside Station
ST3Central Station

STATION: fixes the transitive dependency, StationName no longer depends on OfficerID at all.

STATION:修正传递依赖,StationName 不再依赖 OfficerID。

Same three-step procedure as the inventory example, entirely different domain, entirely different tables, the METHOD is what transfers, not the specific entities.

与库存例子是同样的三步流程,领域完全不同,表也完全不同,可迁移的是方法,而不是具体的实体。

Check your understanding

检查你的理解

In the inventory example, why does SUPPLIER end up as its own separate table?

在库存例子中,为什么 SUPPLIER 最终成为一张单独的表?

Common mistake

常见错误

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

RecurringWhen candidates split a violating attribute into its own new table, they often forget to leave a foreign key behind in the original table. The split fixes the dependency problem, but without the foreign key, the relationship between the two tables is lost entirely, there's no way to tell which SUPPLIER a PRODUCT actually came from anymore.

反复出现当考生把违规的属性拆到新表时,常常忘记在原来的表里留下外键。拆分解决了依赖问题,但没有外键,两张表之间的关系就彻底丢失了,再也无法知道某个 PRODUCT 究竟来自哪个 SUPPLIER。

✕

What a weak answer looks like: A student splits SupplierName and SupplierPhone into a new SUPPLIER table, but PRODUCT is left with no SupplierID at all, only ProductID and ProductName.

较弱的答案是这样的:一位学生把 SupplierName 和 SupplierPhone 拆到一张新的 SUPPLIER 表里,但 PRODUCT 完全没有 SupplierID,只剩下 ProductID 和 ProductName。

✓

What it should say: PRODUCT keeps SupplierID as a foreign key referencing SUPPLIER's primary key, even after SupplierName and SupplierPhone have moved out. The split removes the transitive dependency, it must not also remove the relationship itself.

应该这样写:即使 SupplierName 和 SupplierPhone 已经移出,PRODUCT 仍保留 SupplierID 作为外键,引用 SUPPLIER 的主键。拆分消除了传递依赖,但绝不能同时消除关系本身。

Revision questions

复习题

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

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

Question 1 · 2 marks第 1 题 · 2 分

[2]The unnormalized inventory table above holds several supplier names in one field. State which normal form this breaks and what must change to satisfy it.

[2]上面未规范化的库存表在一个字段中保存了好几个供应商名称。指出这违反了哪个范式,以及必须怎样改变才能满足它。

Question 2 · 4 marks第 2 题 · 4 分

[4]An unnormalized HOSPITAL_APPOINTMENT table has the fields (PatientID, PatientName, DoctorID, DoctorName, DoctorDepartment, AppointmentDate). PatientName depends only on PatientID, and DoctorDepartment depends on DoctorID rather than on the primary key. Identify the type of dependency for each case, and state which normal form each violates.

[4]一张未规范化的 HOSPITAL_APPOINTMENT 表有字段(PatientID, PatientName, DoctorID, DoctorName, DoctorDepartment, AppointmentDate)。PatientName 只依赖 PatientID,而 DoctorDepartment 依赖 DoctorID 而不是主键。分别指出每种情况的依赖类型,并说明各自违反了哪个范式。

Question 3 · 6 marks第 3 题 · 6 分

[6]Construct a database normalized to 3NF for a school's library system, where LOAN(StudentID, StudentName, BookID, BookTitle, BookAuthor, DueDate) is the unnormalized starting table. Show the resulting tables and their primary and foreign keys.

[6]为一所学校的图书馆系统构建一个规范化到 3NF 的数据库,其中 LOAN(StudentID, StudentName, BookID, BookTitle, BookAuthor, DueDate)是未规范化的起始表。给出得到的各张表及其主键和外键。

Flashcards

闪卡

Active recall for this statement's key terms.

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

Prototype render. The construction procedure (1NF/2NF/3NF requirements applied in sequence) is grounded in the IB CS 2027 syllabus's A3.2.6 section; the STOCK_ORDER and CRIME_REPORT worked examples are original, built to reinforce the same procedure across the range of real-world scenarios this statement names, without reproducing a real course example's own scenario. Not yet wired to live routes.