← IB Computer Science

A3.1 · Book pp. 168–173

Database fundamentals

Your level

Your school records, your bank balance and every online shop rest on one idea: keep data in linked tables.

What you need to know

There is one syllabus point, A3.1.1: explain the features, benefits and limitations of a relational database, for both SL and HL.

🔬 Try this first

Write down three pieces of data your school stores about you, then three about each class. What links you to your classes?

Next: tables and keys →

Tables

A database is an organized collection of structured data. A relational database stores it in tables, one for each entity: a person, thing or event you store data about.

AEROPLANE
PlaneIDModelManufacturerEngineEngines
A01Rockwell Commander 112RockwellPiston1
A02Airbus A319 NeoAirbusJet2
A03Boeing 747-100BoeingJet4
A04Boeing 777-8BoeingJet2
A05Boeing 747-100BoeingJet4
Row (tuple, record)One instance, here one aeroplane
Column (attribute, field)One characteristic, here its manufacturer
Primary keyA field unique to each record, here PlaneID. Without it, A03 and A05 could not be told apart
Foreign keyA field that refers to a primary key in another table
Composite keyTwo or more attributes that are unique together

Keys at work

FLIGHTS uses PlaneID as a foreign key. It can repeat there, because one plane makes many flights. PILOTFLIGHT has no single unique field, so FlightID and PilotID together form a composite key.

FLIGHTS
FlightID (primary key)FromToPlaneID (foreign key)
LG8903LUXOTPA03
OS864CAIVIEA04
GB961LHRZRHA03

Next: relationships →

Relationships

A relationship links tables through keys, so that data can be divided up and still connected.

TypeMeaningExample
One-to-one (1:1)A record matches at most one in the other table. RareA country and its capital city
One-to-many (1:m)A record matches many in the other. The most commonA person with several bank accounts
Many-to-one (m:1)The same relationship, seen from the other sideMany staff work for one company
Many-to-many (m:m)Many records match many others. Needs a linking tableActors and movies

💡 Many-to-many

A foreign key holds only one value, so it cannot list many references. A linking table turns one many-to-many into two one-to-many relationships. PILOTFLIGHT does this for flights and pilots.

Referential integrity

A foreign key value must always refer to a record that exists. This stops inconsistent data from being entered.

🔬 Lab: add a flight

Transactions

A transaction is a group of actions treated as one unit. It runs in full or not at all.

🔬 Lab: transfer 500 baht

Account A
Account B

Check yourself: name each relationship
  1. Teachers and homerooms, where each teacher has one homeroom. One-to-one.
  2. Customers and orders. One-to-many.
  3. Students and clubs. Many-to-many, so it needs a linking table.

Next: benefits and limitations →

Benefits

Community supportDecades of use, so help is easy to find
Concurrency controlMany users can work at once without conflict
Data consistencyEveryone sees the same, up-to-date data
Data integrityRules and validation keep data accurate and complete
Data retrievalSQL gives flexible queries with JOIN and WHERE
Reduced duplication and redundancyKeys link tables, so details are stored only once
Reliable transactionsA transaction happens in full or not at all
ScalabilityCan grow by adding CPU, RAM or storage to a server
SecurityUser accounts, permissions and restricted views

Limitations

Big data scalingHard to spread across many machines, and joins slow down at scale
Design complexityTables and relationships need careful planning
Hierarchical dataTrees fit tables poorly, and are slow to traverse
Rigid schemaStructure is set in advance and hard to change
Object-relational mismatchObjects, references and inheritance do not map cleanly to tables
Unstructured dataRecords with no common fields cannot fit a schema

⚠️ Common mistake

General lists of benefits lose marks. Link each point to the scenario in the question, and match the number of marks.

✏️ Book note

The book writes that vertical scaling is cheaper, easier and faster. That is true up to a point, but a single machine has limits. This is why spreading data across many machines (horizontal scaling) matters for big data, and it is what relational databases find hard.

Next: review →

Quick quiz

1. What is a row in a table also called?
2. What is a column also called?
3. What must a primary key be?
4. A foreign key…
5. When do you need a composite key?
6. What does referential integrity prevent?
7. How is many-to-many built in a database?
8. Why are relational databases poor at unstructured data?

Exam-style questions

These are written for this site in the style of Paper 1. Answer on paper first, then open the mark points.

  1. Define the term “database”. [1]

    Mark points
    • An organized collection of structured data that can be accessed in different ways [1].
  2. Define “primary key” and “foreign key”. [2]

    Mark points
    • A primary key uniquely identifies a record in a table [1].
    • A foreign key is an attribute that refers to the primary key of another table [1].
  3. Explain what is meant by referential integrity. [2]

    Mark points
    • Every foreign key value must match an existing primary key value [1].
    • This keeps data consistent during insertions, deletions and updates [1].
  4. A school stores which students take which subjects. Explain how to represent this. [3]

    Mark points
    • It is a many-to-many relationship [1].
    • A foreign key can hold only one value, so a linking table is added [1].
    • It holds StudentID and SubjectID, forming two one-to-many relationships [1].
  5. A music streaming firm is deciding whether to use a relational database for user accounts and billing. Discuss. [4]

    Mark points
    • Reliable transactions mean that a payment completes fully or not at all [1].
    • Integrity rules keep the links between users and payments correct [1].
    • Permissions protect personal billing data [1].
    • However, scaling could be hard with very large numbers of users, though accounts are structured, so relational suits this data [1].