AS Level 9618 · Paper 1 · Chapter 8

Databases

Every system in this course so far has needed somewhere to keep its data, this chapter is about doing that properly: why flat files break down at scale, how a relational database organises data into linked tables, how normalisation removes redundancy by design, what a DBMS actually does for you, and how to read and write the SQL that queries and builds it all. This chapter covers Cambridge 9618 syllabus sections 8.1, 8.2 and 8.3.

13 sections Syllabus 8.1, 8.2, 8.3 6 interactive tools

This chapter covers Cambridge 9618 syllabus section 8.1, the limitations of file-based storage that databases exist to solve, the terminology of the relational model, entity-relationship diagrams, and normalisation up to Third Normal Form; section 8.2, what a Database Management System actually provides on top of raw storage; and section 8.3, the SQL you need to both read and write, covering DDL (CREATE, ALTER) and DML (SELECT, INSERT, UPDATE, DELETE, including JOIN and the aggregate functions and GROUP BY that are easy to miss in a quick read of the syllabus).

8.1

Limitations of the File-Based Approach

Database Concepts

Before databases, data was stored in separate flat files, one per application, each with no awareness of the others. This caused problems serious enough that the relational database was invented specifically to solve them.

ProblemDescriptionExample
Data redundancyThe same data is stored in multiple filesA customer's address stored in both the sales file and the delivery file
Data inconsistencyRedundant copies get out of sync with each otherThe address is updated in one file but not the other
Data isolationHard to access or combine data across multiple filesCross-referencing customer orders with inventory requires manual work
Data dependencyPrograms are tied to a specific file structureChanging the file format breaks every program that uses it
No data integrityNo validation is enforced at the storage levelA file happily stores age = -5 with no error
No concurrent access controlMultiple users accessing the same file at once causes corruptionTwo users edit the same record simultaneously and one edit is silently lost
The database fix
Databases solve all six of these problems at once through centralised data management, defined relationships between tables instead of duplicated copies, integrity constraints enforced by the DBMS itself, and proper concurrent access control.

Try it: watch redundancy break in a file-based system

The customer's address is edited in the sales file below. Click update and watch what happens in the delivery file, which has no idea the sales file even exists.

Interactive tool
File-based redundancy simulator
Two separate flat files both store a copy of the same customer's address, exactly the setup that causes data inconsistency.
A single relational database would store this address once, in one Customer table, and every other table would simply reference the customer by ID.
8.1

Key Database Terminology

Database Concepts

These terms are used precisely and interchangeably with their everyday-spreadsheet equivalents in exam mark schemes, know both names for each.

TermDefinitionSpreadsheet analogy
EntityA real-world object or concept about which data is storedA category, e.g. Student, Course
AttributeA property or characteristic of an entityA column heading, e.g. StudentName
Table / RelationA 2D structure of rows and columns storing entity dataA spreadsheet tab
Record / Tuple / RowOne complete set of related data for a single entity instanceOne row in a spreadsheet
Field / ColumnA single attribute's values for all recordsOne column in a spreadsheet
Primary Key (PK)An attribute, or set of attributes, that uniquely identifies each record in a tableA unique ID number
Foreign Key (FK)An attribute in one table that references the primary key of another, creating a relationshipA link between tables
Composite KeyA primary key made of two or more fields combinedOrderID + ProductID together forms a unique key
IndexA data structure that speeds up searches on a fieldA book's index, letting you find pages faster than reading cover to cover
8.1

Sample Database: School System

Database Concepts

These two linked tables are used as the running example for the rest of this chapter, including the interactive SQL playground later on, get familiar with their structure now.

Student

StudentID 🔑FirstNameLastNameDateOfBirthCourseID 🔗
S001ArjunPatel2007-03-12C01
S002FatimaKhan2006-11-08C02
S003JamesSmith2007-05-22C01
S004SaraAhmed2007-01-15C02

🔑 = Primary Key  |  🔗 = Foreign Key (references the Course table)

Course

CourseID 🔑CourseNameTeacherID 🔗
C01Computer ScienceT03
C02MathematicsT07
8.1

Entity-Relationship Diagrams & Relationships

Database Concepts

Entity-relationship (ER) diagrams show entities and the relationships between them, including cardinality, how many of each entity can relate to the other. There are three cardinalities to know, and they are implemented in a relational database in different ways.

Try it: explore each relationship type

RelationshipMeaningExampleImplementation
One-to-One (1:1)Each A links to at most one B, and vice versaPerson ↔ PassportForeign key placed in either table
One-to-Many (1:M)One A links to many B, but each B links to only one ATeacher → many CoursesForeign key placed in the "many" table
Many-to-Many (M:M)One A links to many B, and one B links to many AStudents ↔ CoursesResolved with a junction table
Many-to-many cannot be built directly
A many-to-many relationship cannot be directly implemented in a relational database, if you tried to put a foreign key in either table, that table would need multiple values in a single field, breaking the relational model entirely. It must be resolved using a junction table (also called a linking table) with a composite primary key, for example a StudentCourse table with StudentID + CourseID together as its primary key, and each as a foreign key referencing its own table.
8.1

Normalisation: First Normal Form (1NF)

Database Concepts
Definition
Normalisation is the process of organising a relational database to reduce data redundancy and improve data integrity. It works by decomposing tables into smaller, well-structured tables, one normal form at a time.

1NF rules

  • Every cell contains a single, atomic (indivisible) value
  • No repeating groups of columns
  • All values in a column are the same data type
  • Each row is unique, and has a primary key
❌ Violates 1NF

Student(ID, Name, Subject1, Subject2, Subject3), repeating groups of columns for what is really the same kind of data.

✅ In 1NF

StudentSubject(StudentID, SubjectName), each row is one student/subject pair, with no repeating columns.

Try it: normalise a table step by step

The unnormalised table below is a real exam-style starting point. Step through 1NF, then continue into 2NF and 3NF in the next section, watching the table split at each stage.

Interactive tool
Normalisation stepper
Starting table: OrderDetail(OrderID, OrderDate, ProductID, ProductName, ProductPrice, Quantity), with OrderID + ProductID repeating for each product ordered.
8.1

Normalisation: 2NF and 3NF

Database Concepts

2NF rules

  • Must already be in 1NF
  • No partial dependencies: every non-key attribute must depend on the whole primary key, not just part of it
  • Only relevant when the primary key is composite, a single-field primary key is automatically in 2NF
❌ Partial dependency

OrderDetail(OrderID, ProductID, ProductName), ProductName depends only on ProductID, not on the full composite key OrderID + ProductID.

✅ Fix

Move ProductName out into its own Product(ProductID, ProductName) table.

3NF rules

  • Must already be in 2NF
  • No transitive dependencies: non-key attributes must not depend on other non-key attributes
❌ Transitive dependency

Student(StudentID, CourseID, CourseName), CourseName depends on CourseID, which is itself not the primary key, not directly on StudentID.

✅ Fix

Separate out a Course(CourseID, CourseName) table, Student keeps only CourseID as a foreign key.

The mnemonic
"The key, the whole key, and nothing but the key." 1NF: the key, every row is unique and identifiable. 2NF: the whole key, no attribute depends on only part of a composite key. 3NF: nothing but the key, no attribute depends on another non-key attribute instead of the key itself.

Continue the stepper from the previous section here, if you have not already reached 3NF.

8.2

Database Management Systems

DBMS

A Database Management System (DBMS) is software that sits between the user (or an application) and the physical data files, handling the creation, maintenance, security and querying of a database so that no program ever has to touch the raw storage directly. This is the layer that turns a pile of files back into the single, consistent, controlled resource that Section 1 showed file-based systems could never be.

FeatureWhat it does
Data dictionaryStores metadata about the database itself, table names, field names, data types, constraints and relationships, so the DBMS always knows the structure of its own data
Data modelling / logical schemaProvides tools to design the logical structure of the database (entities, attributes, relationships) independently of how it is physically stored on disk
Data integrityEnforces constraints automatically, primary key uniqueness, foreign key referential integrity, data type checks, so invalid data is rejected at the point of entry
Data security / access rightsControls who can read, insert, update or delete data, down to individual table or field level, through usernames, passwords and permission levels
Concurrent access controlManages multiple users accessing and editing the database at the same time without one user's changes overwriting another's, usually through locking
Developer interfaceProvides an Application Programming Interface (API) so external programs can connect to, query and update the database without touching the underlying files
Query processorAccepts a query written in a high-level language such as SQL, works out the most efficient way to execute it, and returns the result
One phrase to remember
A DBMS is the software that manages the database. The database itself is just the organised, stored data. In an exam answer, "MySQL is a database" is wrong, MySQL is a DBMS, a piece of software used to create and manage databases.
8.2

ACID Properties of Transactions

DBMS

A transaction is a single logical unit of work made up of one or more database operations, for example "transfer $50 from Account A to Account B" is really two operations, a debit and a credit, that must both succeed or both fail together. A DBMS guarantees four properties for every transaction, remembered by the acronym ACID.

Atomicity

A transaction is treated as a single indivisible unit, either every operation in it completes, or none of them do. If the credit step fails after the debit step succeeded, the whole transaction rolls back and the debit is undone too.

Consistency

A transaction can only take the database from one valid state to another valid state, all rules and constraints (primary keys, foreign keys, data types) must still hold once the transaction finishes.

Isolation

Concurrent transactions must not interfere with each other, the result of running transactions at the same time must be the same as if they had run one after another in some order.

Durability

Once a transaction has been committed, it is permanent, it survives a power cut or system crash immediately afterwards, usually because it has been written to non-volatile storage.

Try it: watch atomicity protect a bank transfer

Below is a $50 transfer from Account A to Account B, modelled as two steps: debit A, then credit B. Run it normally, or simulate a crash between the two steps, and compare what happens with a DBMS enforcing atomicity versus a naive file-based system that has no concept of a transaction at all.

Interactive tool
Transaction atomicity simulator
Transaction: debit $50 from Account A, then credit $50 to Account B. Starting balances: A = $200, B = $100.
Toggle whether atomicity is enforced to see the difference a DBMS makes when a crash happens mid-transaction.
Common exam trap
Do not confuse Consistency with Isolation. Consistency is about the database's rules staying true (a foreign key still points somewhere valid). Isolation is about concurrent transactions not seeing each other's half-finished work. A question describing two users booking the last seat on a flight at the same moment is testing Isolation, not Consistency.
8.3

Data Definition Language (DDL)

SQL

SQL (Structured Query Language) is split into two families of commands: DDL, which defines and modifies the structure of a database (creating and altering databases and tables), and DML, which manipulates the data stored inside that structure (querying, inserting, updating, deleting rows). This section covers DDL.

DDL: Data Definition Language

Defines structure. CREATE, ALTER, DROP. Answers "what does the database look like?"

DML: Data Manipulation Language

Manipulates data. SELECT, INSERT, UPDATE, DELETE. Answers "what data is in it?"

Creating a database and a table

SQL
CREATE DATABASE SchoolSystem;

CREATE TABLE Student (
    StudentID CHARACTER(4) PRIMARY KEY,
    FirstName VARCHAR(20),
    LastName VARCHAR(20),
    DateOfBirth DATE,
    CourseID CHARACTER(3),
    FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);

SQL data types

Data typeStoresExample
CHARACTER(n)Fixed-length text of exactly n characters, padded with spaces if shorterCHARACTER(4) for "S001"
VARCHAR(n)Variable-length text up to n characters, no paddingVARCHAR(20) for a name
BOOLEANTrue or falseIsActive
INTEGERWhole numbersQuantity
REALNumbers with a decimal pointPrice
DATEA calendar date2007-03-12
TIMEA time of day14:30:00

Altering a table after creation

ALTER TABLE changes the structure of a table that already exists, adding or removing columns, or adding keys that were not defined at creation time.

SQL
ALTER TABLE Student ADD Email VARCHAR(40);

ALTER TABLE Student ADD PRIMARY KEY (StudentID);

ALTER TABLE Student
    ADD FOREIGN KEY (CourseID) REFERENCES Course(CourseID);

Try it: build a CREATE TABLE statement

Add columns, pick a data type for each, and mark one as the primary key, the SQL below updates live as you build the table.

Interactive tool
DDL table builder
Generated SQL

8.3

Querying with SELECT

SQL

Every query you write starts from SELECT. The three clauses below, in this order, cover the large majority of exam questions.

SQL
SELECT column1, column2
FROM TableName
WHERE condition
ORDER BY column1 ASC | DESC;
ClausePurpose
SELECTWhich columns to return. SELECT * returns every column
FROMWhich table to read from
WHEREFilters rows by a condition, only matching rows are returned
ORDER BYSorts the result, ASC (default) or DESC

WHERE conditions

SQL
WHERE CourseID = 'C01'
WHERE DateOfBirth > '2006-12-31'
WHERE FirstName LIKE 'A%'
WHERE CourseID = 'C01' AND LastName = 'Smith'
WHERE CourseID = 'C01' OR CourseID = 'C02'
OperatorMeaning
=, <>, >, <, >=, <=Standard comparisons
AND, OR, NOTCombine or negate conditions
LIKEPattern match, % means any number of characters
BETWEEN a AND bInclusive range check
Try it yourself
Section 12 below has a full interactive SQL Query Playground with preset SELECT, WHERE, ORDER BY, GROUP BY and JOIN queries you can run directly against the Student and Course tables, jump to it here.
8.3

Aggregate Functions & GROUP BY

SQL

Aggregate functions perform a calculation across many rows and return a single summary value, instead of returning the rows themselves. They are one of the most commonly missed topics in a quick read of the syllabus, but come up regularly in exam questions.

FunctionReturns
COUNT(column)The number of rows
SUM(column)The total of a numeric column
AVG(column)The average (mean) of a numeric column
MAX(column) / MIN(column)The largest / smallest value
SQL
SELECT COUNT(*) FROM Student;

SELECT CourseID, COUNT(*) AS NumStudents
FROM Student
GROUP BY CourseID;
What GROUP BY actually does
GROUP BY collects rows that share the same value in a column into a single group, and then the aggregate function is calculated once per group rather than once for the whole table. SELECT CourseID, COUNT(*) FROM Student GROUP BY CourseID does not count all students, it counts students separately for each distinct CourseID, producing one output row per course.
Worked example: students per course

Using the Student table from Section 3 (S001/S003 on C01, S002/S004 on C02), SELECT CourseID, COUNT(*) FROM Student GROUP BY CourseID groups the four rows into two groups by CourseID, then counts each group:

CourseIDCOUNT(*)
C012
C022
Exam rule
Every column in the SELECT list must either be inside an aggregate function, or listed in GROUP BY. SELECT FirstName, CourseID, COUNT(*) FROM Student GROUP BY CourseID is invalid, FirstName is neither aggregated nor grouped by, so SQL cannot know which one FirstName to show per group.

Run GROUP BY and aggregate queries yourself in the SQL Query Playground in the next section.

8.3

JOIN, INSERT, UPDATE, DELETE

SQL

The Student and Course tables have been kept separate throughout this chapter, on purpose, that is what normalisation demands. But most useful questions need data from both at once, that is what JOIN is for.

INNER JOIN

SQL
SELECT Student.FirstName, Student.LastName, Course.CourseName
FROM Student
INNER JOIN Course ON Student.CourseID = Course.CourseID;
Reading a JOIN
INNER JOIN Course ON Student.CourseID = Course.CourseID matches every row in Student to the row in Course whose CourseID is equal, producing one combined row per match. This is exactly what the foreign key was for, it is the column that makes the join possible.

INSERT, UPDATE, DELETE

SQL
INSERT INTO Student (StudentID, FirstName, LastName, DateOfBirth, CourseID)
VALUES ('S005', 'Wei', 'Chen', '2007-02-18', 'C01');

UPDATE Student
SET CourseID = 'C02'
WHERE StudentID = 'S005';

DELETE FROM Student
WHERE StudentID = 'S005';
Never forget the WHERE clause
UPDATE Student SET CourseID = 'C02' with no WHERE clause updates every single row in the table, not just one. The same is true of DELETE FROM Student with no WHERE, it deletes every row. This is one of the most common serious mistakes in real SQL use, always double check the WHERE clause before running an UPDATE or DELETE.

Try it: SQL Query Playground

Pick a preset query below and run it against the live Student and Course tables. INSERT, UPDATE and DELETE actually change the displayed data, use Reset to restore the original tables.

Interactive tool
SQL Query Playground
SQL

Results are calculated live in your browser from the Student and Course tables shown in Section 3.
8.1-8.3

Practice Questions

Databases
Q1State two limitations of a file-based approach that a relational database solves.
Model answer

Any two of: data redundancy, the same data duplicated in multiple files; data inconsistency, duplicated copies falling out of sync with each other; data isolation, difficulty combining data held in separate files; data dependency, programs tied to a fixed file structure; no enforced data integrity; and no concurrent access control.

Q2A table OrderDetail(OrderID, ProductID, ProductName, ProductPrice) has a composite primary key of OrderID + ProductID. Which normal form does this violate, and why?
Model answer

It violates Second Normal Form (2NF). ProductName and ProductPrice depend only on ProductID, not on the whole composite key OrderID + ProductID, so this is a partial dependency, non-key attributes must depend on the entire primary key.

Q3Explain why a many-to-many relationship cannot be implemented directly using a foreign key in either table.
Model answer

A foreign key field can only store a single value referencing a single row. In a many-to-many relationship, one record would need to reference several records in the other table simultaneously, which a single field cannot hold without breaking the relational model. A junction table with a composite primary key, holding one row per valid pairing, resolves this instead.

Q4A system crashes mid-transaction, leaving Account A debited but Account B never credited. Which ACID property has been violated, and what should the DBMS do about it?
Model answer

Atomicity has been violated, the transaction was not treated as a single indivisible unit. A DBMS enforcing atomicity should roll the whole transaction back, restoring Account A's original balance, so that either both steps complete or neither does.

Q5Write an SQL statement to find the number of students enrolled on each course, using the Student table from this chapter.
Model answer

SELECT CourseID, COUNT(*) FROM Student GROUP BY CourseID;, this groups the rows by CourseID and counts the rows in each group separately.

Q6Distinguish between DDL and DML, giving one example command from each.
Model answer

DDL (Data Definition Language) defines and modifies the structure of a database, for example CREATE TABLE. DML (Data Manipulation Language) manipulates the data stored inside that structure, for example SELECT or INSERT INTO.

Q7Identify the feature of a DBMS responsible for stopping two users from corrupting the same record when editing it at the same time, and briefly explain how it works.
Model answer

Concurrent access control. It is typically implemented through record locking, when one user begins editing a record, the DBMS locks it so other users cannot edit that same record until the lock is released.

Want the full AS Level 9618 course in one place?
Every chapter, worked examples and practice questions for Cambridge AS Level Computer Science, built to actually make sense.
Browse all notes

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top
Enable Notifications OK No thanks