This handout teaches Theme A3, Databases, for the course first assessed in 2027: relational databases, design and normalisation, and SQL.
Prerequisites: data types from B2 (integer, string, Boolean, real).
International Baccalaureate · IB Diploma · Computer Science · SL · Topic 3
4:39
Databases
A school library keeps loans in one spreadsheet. Each row has the student's name, form, the book and the date. When a student changes form, dozens of rows must…
English narration · English and 中文 subtitles you can switch
This handout teaches Theme A3, Databases, for the course first assessed in 2027: relational databases, design and normalisation, and SQL.
Prerequisites: data types from B2 (integer, string, Boolean, real).
A school library keeps loans in one spreadsheet: each row has the student's name, form, the book's title and author, and the date. When a student changes form, dozens of rows must be edited, and some are missed. Two rows now disagree about the same student. A database is designed to stop this.

A database 数据库 is an organised collection of data. A database management system 数据库管理系统 (DBMS) is the software that stores it, runs queries, controls access and makes backups.
| Problem with flat files | How a relational database helps |
|---|---|
| data redundancy 数据冗余: the same data stored many times | each fact stored once |
| inconsistency 数据不一致: copies disagree | one copy, so nothing to disagree |
| hard to search and combine | SQL queries join tables |
| no control over who sees what | user accounts and permissions |
A relational database 关系数据库 stores data in tables 表 (relations). Each record 记录 (row) is one item; each field 字段 (column, attribute) is one property.
| Key | Meaning | Example |
|---|---|---|
| primary key 主键 | a field whose value is unique for every record | student_id |
| foreign key 外键 | a field that holds the primary key of another table, linking them | student_id in Loan |
| composite key 复合键 | a primary key made of two or more fields | (order_id, item_id) |
Referential integrity 参照完整性 requires a non-null foreign key to match a referenced key (a primary or other suitable unique key); nullable foreign keys may represent no specified reference: a loan cannot refer to a student who does not exist.
| English |
|---|
| database/ˈdeɪtəbeɪs/ |
| database management system/ˈdeɪtəbeɪs ˈmænɪdʒmənt ˈsɪstəm/ |
| data redundancy/ˈdeɪtə rɪˈdʌndənsi/ |
| inconsistency |
| relational database/rɪˈleɪʃənl ˈdeɪtəbeɪs/ |
| table/ˈteɪbl/ |
| record/ˈrekɔːd/ |
| field/fiːld/ |
| primary key/ˈpraɪməri kiː/ |
| foreign key/ˈfɒrən kiː/ |
| composite key/ˈkɒmpəzɪt kiː/ |
| referential integrity/ˌrefəˈrenʃl ɪnˈteɡrɪti/ |
An entity 实体 is a thing we store data about: a student, a book. An entity-relationship diagram 实体关系图 (ERD) shows the entities and how they are related.

| Relationship | Meaning | Example |
|---|---|---|
| one-to-one | each A has one B | a student and their locker |
| one-to-many | one A has many B | one student, many loans |
| many-to-many | many A, many B | students and books |
A many-to-many relationship cannot be stored directly: it is split into two one-to-many relationships through a link table 连接表, here Loan.
Normalisation 规范化 organises tables to remove redundancy. Start from one unnormalised table:

| order_id | customer | phone | items |
|---|---|---|---|
| 501 | Li Na | 1381 | pen, ruler |
| 502 | Li Na | 1381 | pen |
| Form | Rule | Result |
|---|---|---|
| first normal form 第一范式 (1NF) | every field holds one atomic value; no repeating groups | one row per item: key (order_id, item) |
| second normal form 第二范式 (2NF) | 1NF, and no non-key field depends on only part of a candidate key | customer depends only on order_id: move it to an Order table; price depends only on item: move it to an Item table |
| third normal form 第三范式 (3NF) | 2NF, with no transitive dependency of a non-key field on a candidate key | phone depends on the customer, not the order: move it to a Customer table |
Result in 3NF:
Now Li Na's phone number is stored once. A short test for 3NF: every non-key field depends on "the key, the whole key, and nothing but the key".
Each field needs a type, such as INTEGER, VARCHAR(50) (text up to 50 characters), DATE, BOOLEAN or DECIMAL(6,2). Phone numbers are stored as text, not integers: they can start with 0 and are never added up.
| English |
|---|
| entity/ˈentɪti/ |
| entity-relationship diagram/ˈentɪti rɪˈleɪʃənʃɪp ˈdaɪəɡræm/ |
| link table/lɪŋk ˈteɪbl/ |
| normalisation/ˌnɔːməlaɪˈzeɪʃn/ |
| first normal form |
| second normal form |
| third normal form |
SQL 结构化查询语言 (Structured Query Language) defines and queries relational databases. The examples use these tables:

| Student | ||
|---|---|---|
| student_id | name | year_group |
| 1 | Ana | 11 |
| 2 | Ben | 12 |
| 3 | Chen | 11 |
| Book | ||
|---|---|---|
| book_id | title | author |
| 10 | Dune | Herbert |
| 11 | Emma | Austen |
| 12 | Holes | Sachar |
| Loan | |||
|---|---|---|---|
| loan_id | student_id | book_id | date_out |
| 100 | 1 | 10 | 2026-09-01 |
| 101 | 1 | 12 | 2026-09-05 |
| 102 | 3 | 11 | 2026-09-06 |
SELECT name
FROM Student
WHERE year_group = 11
ORDER BY name DESC;
Result: Chen, Ana. WHERE filters rows; ORDER BY … DESC sorts Z to A.
SELECT Student.name, Book.title
FROM Loan
JOIN Student ON Loan.student_id = Student.student_id
JOIN Book ON Loan.book_id = Book.book_id;
Result: (Ana, Dune), (Ana, Holes), (Chen, Emma). A join 连接 combines tables by matching the foreign key with the primary key. Ben has no loans, so he does not appear.
SELECT student_id, COUNT(*) AS loans
FROM Loan
GROUP BY student_id;
Result: (1, 2), (3, 1). Aggregate functions 聚合函数 such as COUNT, SUM, AVG, MAX and MIN work on groups of rows.
| Clause | Use |
|---|---|
WHERE |
filter rows: =, <>, >, LIKE 'D%', AND, OR |
ORDER BY |
sort, ASC or DESC |
GROUP BY |
make groups for aggregate functions |
HAVING |
filter groups: HAVING COUNT(*) > 1 |
INSERT INTO Book (book_id, title, author) VALUES (13, 'Wonder', 'Palacio');
UPDATE Student SET year_group = 12 WHERE student_id = 3;
DELETE FROM Loan WHERE loan_id = 100;
An UPDATE or DELETE without WHERE changes every row.
A transaction 事务 is a group of operations that must all succeed or all fail, such as moving money: take it from one account and add it to another. Transactions follow ACID:

| Property | Meaning |
|---|---|
| atomicity | all of it happens, or none of it |
| consistency | the database moves from one valid state to another |
| isolation | transactions running at once do not interfere |
| durability | once committed, a change survives a crash |
Each source row contributes one result per matching record. A left join retains an unmatched source row as one NULL-matched row.
| English |
|---|
| SQL/ˌes kjuː ˈel/ |
| join/dʒɔɪn/ |
| aggregate function/ˈæɡrɪɡeɪt ˈfʌŋkʃn/ |
| transaction/trænˈsækʃn/ |
booking_id, room, room_capacity, date). Explain why it is not in 3NF and normalise it.SELECT COUNT(*) FROM Loan WHERE book_id > 10; for the tables above.Answers.
book_id in Book. A foreign key is a field holding another table's primary key, such as book_id in Loan.room_capacity depends on room, a non-key field: a transitive dependency. Split into Booking(booking_id, room, date) and Room(room, room_capacity).SELECT title FROM Book WHERE author = 'Austen';SELECT Student.name, COUNT(*) FROM Student JOIN Loan ON Student.student_id = Loan.student_id WHERE Student.year_group = 11 GROUP BY Student.name; giving (Ana, 2), (Chen, 1).UPDATE Book SET author = 'L. Sachar' WHERE book_id = 12;ON condition matching the foreign key to the primary key.UPDATE and DELETE statements for a WHERE clause.Loading subjects…