A3.1.1 · Database fundamentals
A3.1.1 · 数据库基础
Relational databases: features, benefits and limitations
关系型数据库:特点、优点与局限
A relational database is just tables, connected by keys, but that one idea explains almost everything a real database can do, and can't.
关系型数据库不过是用键连接起来的表,但就这一个想法,几乎就解释了真正的数据库能做的一切,以及不能做的。
Explainer
讲解
Think of a school keeping two separate binders: one listing every student, one listing every subject they take. If a student changes their name, someone has to find and fix it in both binders separately, unless the two binders are linked by something as simple as a shared ID number. That link is the entire idea behind a relational database.
想象一所学校有两本独立的活页夹:一本列出每一位学生,另一本列出他们所修的每一门科目。如果某位学生改了名字,就得有人分别在两本活页夹里找到并改掉,除非两本之间用像共享编号这样简单的东西连起来。这个联系,就是关系型数据库的全部思想。
This whole topic answers one question, A3's own guiding question: what are the principles, structures and operations that form the basis of database systems? This statement builds the foundation everything else in A3 sits on: what a relational database actually is, before later statements cover schemas, ERDs and normalization.
这整个主题回答的是一个问题,也就是 A3 自己的引导性问题:构成数据库系统基础的原理、结构和操作是什么?这条陈述为 A3 的其余内容打下地基:关系型数据库究竟是什么,然后后面的陈述才会讲到模式、ERD 和规范化。
A relational database holds everything in tables: a grid in which each column is one attribute and each row is one record. Each table describes one entity (a person, a flight, a product). Each row (a tuple or record) is one instance of that entity. Each column (an attribute or field) is one characteristic of it.
关系型数据库把所有东西存放在表中:一张网格,每一列是一个属性,每一行是一条记录。每张表描述一个实体(一个人、一次航班、一件商品)。每一行(元组或记录)是该实体的一个实例。每一列(属性或字段)是它的一个特征。
A primary key is the field a table uses to tell its rows apart. Pick any value it holds and exactly one row answers to it, which is why no two rows are ever allowed to share one. A foreign key takes that same idea and points it outward: a field in one table copies the primary key value from a row in a completely different table, and that copied value is the connection, the thing that turns two separate tables into one linked structure.
主键是表用来区分各行的字段。取它所保存的任何一个值,都恰好只有一行对应它,这就是为什么绝不允许两行共享同一个主键值。外键把同样的思想指向外部:一张表中的字段复制另一张完全不同的表中某一行的主键值,而这个被复制的值就是连接,它把两张独立的表变成一个相互关联的结构。
Reading crow's-foot notation
读懂鸡爪(crow's-foot)记号
Relationships between tables are drawn as lines with a symbol at each end, never just an arrow. The symbol names how many records on THAT side can take part. The worked example below uses the plain "one" and "many" symbols; the other four are worth recognising too, since ERDs (a later statement) use all six.
表与表之间的关系画成两端各带一个符号的线,而不只是一个箭头。符号说明那一侧有多少条记录可以参与。下面的例题用的是最基本的"一"和"多"符号;其余四种也值得认识,因为后面讲的 ERD 会用到全部六种。
One. Exactly one record on this side, no more, no fewer.
一。这一侧恰好一条记录,不多也不少。
OneMany. Any number of records on this side, including several at once.
多。这一侧任意数量的记录,包括同时有好几条。
Many多One and only one. The stricter version of "one": this record must exist, and there can never be more than one.
一且仅一。比"一"更严格的版本:这条记录必须存在,而且永远不能多于一条。
One (and only one)一且仅一Zero or one. This side is optional: a matching record might not exist at all, but if it does, there's only ever one.
零或一。这一侧是可选的:匹配的记录可能根本不存在,但如果存在,就只会有一条。
Zero or one零或一One or many. At least one record must exist on this side, and there can be more.
一或多。这一侧至少必须存在一条记录,也可以更多。
One or many一或多Zero or many. Fully optional AND unlimited: no matching record is required, but there's no upper limit either.
零或多。完全可选而且没有上限:不要求有匹配的记录,但数量也没有上限。
Zero or many零或多Worked example
例题讲解
An airline's database: four tables, three relationships
一家航空公司的数据库:四张表,三种关系
AEROPLANE lists every plane. FLIGHTS lists every flight, and needs to know which plane flew it. PILOTS lists every pilot. PILOTFLIGHT links flights to the pilots who flew them, since one flight can have more than one pilot, and one pilot flies more than one flight.
AEROPLANE 列出每架飞机。FLIGHTS 列出每次航班,并且需要知道是哪架飞机执飞的。PILOTS 列出每位飞行员。PILOTFLIGHT 把航班与执飞它们的飞行员连接起来,因为一次航班可以有不止一位飞行员,而一位飞行员也执飞不止一次航班。
| Model |
| Manufacturer |
| Engine |
| Departure |
| Destination |
| Name |
Click any PK badge to see which foreign keys reference it. The connectors use real crow's-foot notation: a single bar (⊣) at the "one" end, a crow's foot (❮) at the "many" end.
点击任一个 PK 徽章,查看有哪些外键引用了它。连接线使用真正的鸡爪记号:"一"的一端是一条竖杠(⊣),"多"的一端是鸡爪(❮)。
Click a key badge above to explore the relationships.点击上面的键徽章,探索它们之间的关系。
PILOTFLIGHT has no primary key of its own, on purpose, since neither FlightID nor PilotID is unique alone. What makes that pair unique, and how it's different from a concatenated key, is worth its own look after the check below.
PILOTFLIGHT 故意没有自己的主键,因为 FlightID 和 PilotID 单独都不是唯一的。是什么让这一对值唯一,以及它与拼接键有何不同,在下面的检测之后值得单独看一看。
The four relationship types
四种关系类型
One record relates to exactly one in another table. The rarest of the four in real designs, most pairs of tables need more flexibility than this, a passport record tied to exactly one holder is the kind of case where it genuinely fits.
一条记录恰好与另一张表中的一条记录相关。这是现实设计中最少见的一种,大多数表之间需要更灵活的关系,一份护照记录恰好对应一位持有人,就是它真正适用的情形。
One record relates to several in another table. AEROPLANE → FLIGHTS: one plane flies many flights.
一条记录与另一张表中的多条记录相关。AEROPLANE → FLIGHTS:一架飞机执飞多次航班。
The same relationship, viewed from the other side. FLIGHTS → AEROPLANE: many flights share one plane.
同样的关系,从另一侧来看。FLIGHTS → AEROPLANE:多次航班共用一架飞机。
Many records relate to many others. FLIGHTS ↔ PILOTS, solved above with a linking table.
多条记录与其他多条记录相关。FLIGHTS ↔ PILOTS,上面用一张连接表解决了。
Referential integrity is the rule that keeps this whole structure honest: a foreign key must always match a real, existing primary key somewhere. A FLIGHTS row can't reference a PlaneID that doesn't exist in AEROPLANE. This is what stops insertion, deletion and updates from quietly breaking the relationships between tables.
参照完整性(referential integrity)是让整个结构保持可信的规则:外键必须始终匹配某处一个真实存在的主键。FLIGHTS 中的一行不能引用 AEROPLANE 中不存在的 PlaneID。这正是它能防止插入、删除和更新悄悄破坏表与表之间关系的原因。
Check your understanding
检查你的理解
What is the relationship between FLIGHTS and PILOTFLIGHT specifically (not FLIGHTS and PILOTS overall)?
具体来说(不是 FLIGHTS 和 PILOTS 整体),FLIGHTS 与 PILOTFLIGHT 之间是什么关系?
Explainer
讲解
Composite keys and concatenated keys
复合键与拼接键
PILOTFLIGHT has no primary key of its own, on purpose. FlightID alone repeats (one flight, multiple pilots) and PilotID alone repeats too (one pilot, multiple flights). Together, though, the PAIR (FlightID, PilotID) is always unique, that pair is PILOTFLIGHT's composite key, and this pattern, a "linking table" sitting between FLIGHTS and PILOTS, is the standard way to represent a many-to-many relationship, which a single foreign key can never do on its own.
PILOTFLIGHT 故意没有自己的主键。FlightID 单独会重复(一次航班,多位飞行员),PilotID 单独也会重复(一位飞行员,多次航班)。然而这一对 (FlightID, PilotID) 合在一起总是唯一的,这一对就是 PILOTFLIGHT 的复合键,而这种放在 FLIGHTS 和 PILOTS 之间的"连接表"模式,是表示多对多关系的标准方式,单靠一个外键永远做不到这一点。
Check that claim with three real rows. Suppose PILOTFLIGHT actually held this data:
用三行真实数据来验证这个说法。假设 PILOTFLIGHT 实际保存了这些数据:
Neither column can be the primary key by itself, both repeat. The PAIR never repeats. That's exactly what makes it a composite key.
两列单独都不能作为主键,因为都会重复。而这一对永远不重复。这正是它成为复合键的原因。
Composite key vs concatenated key, they're not the same thing. A composite key keeps the fields separate (PILOTFLIGHT above still stores FlightID and PilotID as two distinct columns). A concatenated key goes a step further: it combines the VALUES from several attributes into one single new field, so a table with a name and a club title, for example, could concatenate them into one field like "Jane Doe-Arts and Crafts" to act as a single primary key column, not two.
复合键与拼接键,它们不是一回事。复合键让各个字段保持分开(上面的 PILOTFLIGHT 仍然把 FlightID 和 PilotID 存为两个独立的列)。拼接键(concatenated key)则更进一步:它把几个属性的值合并成一个新字段,例如一张包含姓名和社团名称的表,可以把它们拼成一个字段,如 "Jane Doe-Arts and Crafts",作为单独一个主键列,而不是两个。
Worked from scratch, on a TEACHER table where no single field is unique alone (two teachers could share a name; the same club could have several teachers across years):
用一张 TEACHER 表从头演示一遍,其中没有任何单个字段是唯一的(两位教师可能同名;同一个社团在不同年份可能有好几位教师):
Compare that to the composite key above: PILOTFLIGHT keeps two separate columns, FlightID and PilotID, and checks both together. TEACHER's concatenated key has collapsed the two pieces of information into ONE column, there's no second field left to check, the whole identity lives in that single concatenated string.
与上面的复合键比较:PILOTFLIGHT 保留两个独立的列 FlightID 和 PilotID,并把两者合在一起检查。TEACHER 的拼接键则把两条信息压缩进了一个列,没有第二个字段可以检查了,整个身份都存在于那一个拼接起来的字符串里。
Check your understanding
检查你的理解
PILOTFLIGHT is uniquely identified by the pair (FlightID, PilotID), stored as two separate columns. Is this a composite key or a concatenated key?
PILOTFLIGHT 由(FlightID, PilotID)这一对值唯一标识,存为两个独立的列。这是复合键还是拼接键?
Explainer
讲解
Benefits and limitations
优点与局限
Every one of these is a real, nameable consequence of the tables-and-keys structure above, not just an abstract claim. The syllabus names ten benefits and six limitations specifically.
这里的每一条都是由上面的"表加键"结构带来的、真实且可以点名的结果,而不只是抽象的说法。考纲具体列出了十条优点和六条局限。
Benefits
优点
- Community support: the most widely used database model since the 1970s, so help is easy to find.
- 社区支持:自 20 世纪 70 年代以来使用最广泛的数据库模型,所以很容易找到帮助。
- Concurrency control: manages simultaneous users without their changes conflicting.
- 并发控制:管理同时使用的多个用户,使他们的改动不会冲突。
- Data consistency: each fact is stored in one place, so every user sees the same value.
- 数据一致性:每个事实只存放在一处,所以每个用户看到的值都相同。
- Data integrity: the data stays accurate and untampered across its whole lifecycle.
- 数据完整性:数据在整个生命周期中保持准确,不被篡改。
- Data retrieval: SQL queries (SELECT, JOIN, WHERE) retrieve exactly what's needed.
- 数据检索:SQL 查询(SELECT、JOIN、WHERE)能准确取出所需要的数据。
- Reduced data duplication: shared keys link tables instead of repeating full details.
- 减少数据重复:用共享的键连接各张表,而不是重复完整的细节。
- Reduced redundancy: normalizing into separate tables avoids storing the same fact twice.
- 减少冗余:规范化为独立的表,避免同一事实被存两次。
- Reliable transaction processing: a transaction runs fully, or not at all, never half-done.
- 可靠的事务处理:一个事务要么完整执行,要么完全不执行,绝不会只做一半。
- Scalability: can grow by adding resources (CPU, RAM, storage) to the existing system.
- 可扩展性:可以通过给现有系统增加资源(CPU、内存、存储)来扩大规模。
- Security features: user accounts and permissions control exactly who can access what.
- 安全特性:用户账号和权限能精确控制谁可以访问什么。
Limitations
局限
- "Big data" scalability issues: joins and complex queries slow down as data grows very large.
- "大数据"扩展性问题:数据变得非常庞大时,连接(join)和复杂查询会变慢。
- Design complexity: tables and relationships need careful planning before anything is built.
- 设计复杂:在构建任何东西之前,表和关系都需要仔细规划。
- Hierarchical data handling: tree-shaped data (like an org chart) is awkward to store as flat tables.
- 层次化数据的处理:树状数据(比如组织结构图)很难存成扁平的表。
- Rigid schema: the structure is defined upfront, and changing it later is slow and complicated.
- 模式僵硬:结构在一开始就已定义,以后修改既慢又复杂。
- Object-relational impedance mismatch: OOP concepts (like inheritance, pointers) don't translate cleanly into tables.
- 对象-关系阻抗不匹配:OOP 的概念(如继承、指针)无法干净地转换成表。
- Unstructured data handling: data with no consistent fields across records can't fit a fixed schema at all.
- 非结构化数据的处理:各条记录没有统一字段的数据,根本放不进固定的模式里。
Common mistake
常见错误
A3.1.1A3.1.1 的这一常见模式RecurringWhen a question gives a specific scenario and asks for benefits or limitations, markers reported candidates answering with generic, memorised points that never actually connect back to the scenario given. Full marks require tying the benefit or limitation to the specific situation in the question, not just naming it in the abstract.
反复出现当题目给出具体情境并要求写出优点或局限时,阅卷员反馈说,考生用的是笼统、死记硬背的要点,从未真正与给定情境联系起来。要拿满分,必须把优点或局限与题目中的具体情形联系起来,而不只是抽象地点出名称。
What a weak answer looks like: "A benefit of a relational database is security features." True, but generic, it never mentions the airline scenario at all.
较弱的答案是这样的:"关系型数据库的一个优点是安全特性。"没错,但太笼统,根本没有提到航空公司这个情境。
What it should say: "The airline's database can restrict access so that only authorized ground staff can update FLIGHTS records, preventing an unauthorized user from changing a flight's departure time by mistake." Names the benefit AND applies it to the actual scenario.
应该这样写:"航空公司的数据库可以限制访问,使只有获得授权的地勤人员才能更新 FLIGHTS 记录,防止未经授权的用户误改某次航班的出发时间。"点出了优点,并且把它应用到了实际情境中。
Revision questions
复习题
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
仿真题风格。先写出你的答案,再显示评分要点并给自己打分。
[2]In the airline database, state which field is the primary key of FLIGHTS and which field in FLIGHTS is a foreign key, giving a reason for each.
[2]在航空公司数据库中,指出 FLIGHTS 的主键是哪个字段,以及 FLIGHTS 中哪个字段是外键,并分别说明理由。
Marking points
评分要点
- FlightNo (or equivalent) is the primary key of FLIGHTS: it is unique for every flight and never null.
- AeroplaneID in FLIGHTS is a foreign key: it holds the primary key value of a row in AEROPLANE, linking each flight to the aircraft flying it.
- Reason for the foreign key: it lets one aircraft be referenced by many flights without repeating the aircraft's details.
- FlightNo(或等价字段)是 FLIGHTS 的主键:它对每个航班唯一且永不为空。
- FLIGHTS 中的 AeroplaneID 是外键:它存放 AEROPLANE 某一行的主键值,把每个航班与执飞的飞机关联起来。
- 外键的理由:它让一架飞机可以被多个航班引用,而不必重复飞机的详细信息。
[2]A staff member tries to delete an AEROPLANE record while FLIGHTS still refers to it. Explain what the database should do, and why.
[2]一位员工试图删除一条 AEROPLANE 记录,而 FLIGHTS 仍然引用着它。解释数据库应该怎么做,以及为什么。
Marking points
评分要点
- The database should refuse the deletion (or cascade it), because referential integrity must be preserved.
- If the AEROPLANE row were removed, the AeroplaneID in the FLIGHTS rows would point to a record that no longer exists.
- That would leave orphaned foreign keys and queries joining the two tables would lose those flights or error.
- 数据库应拒绝该删除(或对其做级联),因为必须保持参照完整性。
- 如果 AEROPLANE 那一行被移除,FLIGHTS 各行中的 AeroplaneID 就会指向一条不再存在的记录。
- 那会留下孤立的外键,连接两表的查询会丢失那些航班或报错。
[6]A veterinary clinic keeps a database of PETS and APPOINTMENTS, where one pet can have many appointments. Discuss one benefit and one limitation of using a relational database for this scenario.
[6]一家兽医诊所保存着 PETS 和 APPOINTMENTS 的数据库,其中一只宠物可以有多个预约。讨论在这个情境中使用关系型数据库的一个优点和一个局限。
Marking points
评分要点
- Benefit: reduced duplication and enforced integrity. Each pet's details are stored once in PETS and each appointment links to it by PetID, so a pet cannot be booked in twice or misspelled per appointment.
- Benefit: flexible querying with SQL, for example every appointment for one pet, or all pets due a checkup this month, without changing the structure.
- Limitation: the schema is rigid. Adding a new kind of data (a second owner, a vaccination history) means altering tables and possibly existing queries.
- Limitation: joins across PETS and APPOINTMENTS cost processing time, which grows as the data grows.
- Limitation: it fits structured data well but poorly for unstructured items such as scanned X-ray images or free-form clinical notes.
- Judgement: for this clearly structured, relational scenario the benefits outweigh the limitations.
- 优点:减少重复并强制完整性。每只宠物的详细信息在 PETS 中只存一次,每次预约通过 PetID 关联到它,所以一只宠物不会被重复登记或在各预约中拼写不一致。
- 优点:用 SQL 灵活查询,例如某只宠物的所有预约,或本月所有需要体检的宠物,而无需更改结构。
- 局限:模式是刚性的。增加一种新数据(第二位主人、疫苗接种史)意味着修改表,可能还要改现有查询。
- 局限:跨 PETS 和 APPOINTMENTS 的连接消耗处理时间,并随数据增长而增大。
- 局限:它很适合结构化数据,但不适合非结构化内容,如扫描的 X 光片或自由格式的临床记录。
- 判断:对于这个明显结构化、关系型的场景,优点大于局限。
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.1.1, and every other IB CS 2027 statement.
免费账号:解锁 A3.1.1 以及其他所有 IB CS 2027 陈述的全部复习题和闪卡。