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

A3.2.3 · Database design

A3.2.3 · 数据库设计

Data types in relational databases

关系数据库中的数据类型

A UK phone number saved as an INTEGER quietly loses its leading zero, the wrong data type doesn't just look untidy, it can genuinely break a database.

一个英国电话号码若以 INTEGER 类型保存,会悄悄丢失开头的 0,错误的数据类型不只是看起来不整洁,它真的会破坏数据库。

Core for SL & HL SL 与 HL 核心内容 Command term: outline 命令词:outline(概述) Builds on A3.2.1 & A3.2.2 建立在 A3.2.1 与 A3.2.2 基础之上

Explainer

讲解

A phone number "0207 946 0958" stored as INTEGER becomes 2,079,460,958, the leading zero is gone, and the number no longer prints correctly on anything. It happened because a data type was chosen that fit the DIGITS but not the actual PURPOSE of the field, phone numbers are never added or multiplied, so they should never have been numeric in the first place.

电话号码"0207 946 0958"若以 INTEGER 类型存储,会变成 2,079,460,958,开头的 0 消失了,该号码从此在任何地方都无法正确显示。这是因为所选的数据类型只匹配了字段的数字外形,却不符合该字段实际的用途:电话号码从来不会被相加或相乘,因此它们本就不应该被当作数值处理。

Df

A data type is assigned to every attribute (field) in a table, and it does two jobs at once: it restricts which VALUES the field will accept, and it determines which OPERATIONS are actually permitted on that field, arithmetic, date comparisons, and so on.

数据类型会被赋予表中的每一个属性(字段),它同时承担两项工作:限制该字段可以接受哪些值,并决定该字段实际允许执行哪些操作,例如算术运算、日期比较等。

The seven relational data types

关系数据库的七种数据类型

Data type数据类型 Meaning含义
CHARACTER(n)Text of a fixed width: every value occupies n characters, shorter ones padded out to fit.固定宽度的文本:每个值都占用 n 个字符,较短的值会被填充以凑满长度。
VARCHAR(n)Text of a varying width, where n sets the ceiling rather than the size.宽度可变的文本,其中 n 设定的是上限而非实际大小。
INTEGERA counting number, with nothing after the point.整数,小数点后没有数字。
REALA number able to carry a fractional part.可以带有小数部分的数字。
DATEA calendar date, held in the form YYYY-MM-DD.日历日期,以 YYYY-MM-DD 的形式保存。
TIMEA time of day, held in the form HH:MM:SS.一天中的时间,以 HH:MM:SS 的形式保存。
BOOLEANOne of exactly two values, true or false.只能是两个值之一:true(真)或 false(假)。

Why the choice actually matters

为什么这个选择真的很重要

!

Storage and indexing efficiency. When a field's length genuinely never changes, a fixed-width UserID is the classic case, CHARACTER(n) can reserve exactly that much space and be done with it. VARCHAR(n) has to keep track of how long each individual value actually is, and that bookkeeping is work a truly fixed-length field simply never has to do.

存储与索引效率。当一个字段的长度确实永远不变时(固定宽度的 UserID 就是典型例子),CHARACTER(n) 可以精确预留这么多空间,不再多做任何事。VARCHAR(n) 则必须记录每个值实际的长度,而这项记录工作对于真正的定长字段来说根本不需要。

!

Which operations are even possible. A value's data type is what tells the database whether an operation makes sense at all. Give it a price or a quantity typed as plain text instead of REAL or INTEGER, and adding two of them together stops being something the database can do, the application is left converting that text back into a real number itself, on every single calculation, forever.

哪些操作根本上是可行的。值的数据类型决定了某项操作对数据库而言是否有意义。如果把价格或数量当作纯文本而非 REAL 或 INTEGER 来存储,那么把两个值相加就不再是数据库能做的事,应用程序只能自行把这些文本转换回真正的数字,而且每次计算都要重新转换一次,永无止境。

!

Insertion errors. A field's declared type is a promise the DBMS actually enforces, not just a suggestion. Try to insert "twenty" into a field typed INTEGER and the insert is refused outright, loudly, at the moment it happens, rather than being quietly accepted and causing confusion somewhere down the line.

插入错误。字段声明的类型是 DBMS 真正会强制执行的约定,而不仅仅是一种建议。如果尝试把"twenty"插入一个类型为 INTEGER 的字段,插入操作会在发生的当下就被直接、明确地拒绝,而不是被悄悄接受,并在之后的某个环节造成混乱。

?

Data type consistency is easy to break without noticing, since the database and the application code that talks to it are two separate places the same attribute's type has to agree. Let them drift apart, one side treating a field as an integer, the other as text, and the failure often isn't a clean error at all, it's a query that runs and returns something quietly wrong.

数据类型一致性很容易在不知不觉中被破坏,因为数据库与和它交互的应用程序代码是两个独立的地方,它们对同一属性的类型必须保持一致。一旦两者出现偏差,一边把某字段当作整数处理,另一边当作文本处理,由此产生的故障往往不是一个干净利落的错误,而是一条能正常执行、却悄悄返回错误结果的查询。

Worked example

示例讲解

Designing an EVENT table, field by field

逐字段设计 EVENT 表

An EVENT table for a ticketing system needs seven fields, conveniently, one of each data type. Click any field to see the type that fits it, and what specifically would go wrong with a plausible wrong choice.

一个售票系统的 EVENT 表需要七个字段,恰好每种数据类型各对应一个。点击任意字段,即可查看适合它的数据类型,以及一个看似合理的错误选择具体会出什么问题。

EVENT

Click a field name to see its correct data type and a realistic wrong choice.

点击字段名称,查看其正确的数据类型以及一个真实的错误选择。

Click a field above to see its reasoning.

点击上方的字段以查看其推理过程。

Check your understanding

检验你的理解

Every EventID is formatted as exactly 6 characters, e.g. "EVT045". Which data type is the more efficient choice, and why?

每一个 EventID 的格式都恰好是 6 个字符,例如"EVT045"。哪种数据类型是更高效的选择?为什么?

Common mistake

常见错误

Markers reported this pattern for A3.2.3 考官报告了 A3.2.3 中出现的这一模式

RecurringThe command term is "outline," which needs a brief explanation, not just a label. Candidates often name the correct data type and stop there, without stating the actual CONSEQUENCE of getting it wrong, which is exactly what earns the marks on this statement.

反复出现命令词是 outline(概述),这需要一段简要的说明,而不仅仅是一个名称。考生常常只说出正确的数据类型就停下,没有说明选错时实际会产生的后果,而这正是本条陈述得分的关键所在。

✕

What a weak answer looks like: "TicketPrice should be REAL." Names the right type, but says nothing about why, no marks for the reasoning half of the question.

薄弱答案的样子: "TicketPrice 应该是 REAL。" 说出了正确的类型,但完全没有说明原因,题目中考察推理的那一半得不到分数。

✓

What it should say: "TicketPrice should be REAL, not VARCHAR, because storing it as text would prevent the system from calculating order totals or applying discounts without first converting every value to a number." States the type AND the concrete effect of getting it wrong.

应该这样作答: "TicketPrice 应该是 REAL,而不是 VARCHAR,因为以文本形式存储会导致系统在计算订单总额或应用折扣之前,必须先把每一个值都转换成数字。" 既说明了正确的类型,也说明了选错时的具体影响。

Revision questions

复习题

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

采用历年真题风格。写下答案,然后展开评分要点给自己打分。

Question 1 · 2 marks 第 1 题 · 2 分

[2]Outline the most appropriate data type for a field storing a person's date of birth, and one reason for your choice.

[2]概述用于存储个人出生日期的字段最合适的数据类型,并说明你选择的一个理由。

Question 2 · 3 marks 第 2 题 · 3 分

[3]A database stores a product's InStock status as VARCHAR, holding values like "yes", "Yes" and "in stock" across different records. Outline two problems this could cause.

[3]某数据库将产品的 InStock(库存)状态以 VARCHAR 存储,不同记录中出现"yes"、"Yes"、"in stock"等不同的值。概述这可能导致的两个问题。

Question 3 · 4 marks 第 3 题 · 4 分

[4]A school's own records store a StudentID as INTEGER, but the exam board's system stores the same StudentID as VARCHAR. Explain two problems this data type mismatch could cause when the two systems exchange data.

[4]某学校自己的记录将 StudentID 存储为 INTEGER,但考试局系统将同一个 StudentID 存储为 VARCHAR。解释这种数据类型不匹配在两个系统交换数据时可能导致的两个问题。

Flashcards

记忆卡片

Active recall for this statement's key terms.

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

Prototype render. Content grounded in the IB CS 2027 syllabus's A3.2.3 section (all seven data types, the indexing/operations/insertion/consistency effects). Not yet wired to live routes.