Databases 🗄️
Tables, records and fields; data types, validation and primary keys; and SQL — the language that pulls exactly the information you need out of a database. Write real queries and watch the table rearrange itself in 3D.
🗄️ What is a database? 9.1.1
A single-table database contains just one table — that's all IGCSE needs (relational databases with many tables come at A Level). Why use one?
- ✏️ A change or addition is made once, so the data stays consistent.
- 👥 Everyone uses the same data.
- 1️⃣ In a relational database each item is stored only once — no duplication.
Databases store information about people (patients in a hospital, pupils at a school), things (cars for sale, library books) and events (hotel bookings, race results).
🧱 Tables, records and fields 9.1.1
Records can be added and deleted, so the number of records changes. The number of fields is fixed — every record has the same fields.
🔬 Table anatomy
Click any value: its record (row) and its field (column) light up.
✅ Validation in a database 9.1.1
Validation (Chapter 7) checks data is reasonable before it's stored. Some checks are automatic — the database software rejects an impossible date in a Date/Time field such as DateOfAdmission. Others must be set up by the database developer before the database is used — for example a range check so WardNumber only accepts 1 to 10. Try both in the record form on the next tab.
🏷️ Basic data types 9.1.2
Every field needs a data type: it classifies how the data is stored and displayed and which operations can be done on it (an integer is stored and shown as a whole number and can be used in calculations). The syllabus names six; Access uses slightly different names:
| Syllabus data type | Holds | Access data type | Example field |
|---|---|---|---|
| text / alphanumeric | A number of characters | Short Text / Long Text | FamilyName |
| character | A single character | Short Text, field size 1 | Gender |
| Boolean | One of two values: True/False, 1/0, Yes/No | Yes/No | InProduction |
| integer | A whole number | Number, fixed, 0 decimal places | WardNumber |
| real | A decimal number | Number, decimal | Price |
| date/time | A date and/or a time | Date/Time | DateOfAdmission |
🔑 Primary keys 9.1.3
An existing field can be the primary key if it's unique — the ISBN in a BOOK table. In the PATIENT table every existing field could repeat (two patients called Sing, two in bed 8, many in ward 6), so a new field is added: HospitalNumber, e.g. HN284511.
📝 Add a patient — validation and the primary key at work
Every rule below has been set up on the PATIENT table. Type a new record: a value must pass its checks before the record can be saved. Then query your new patient on the SQL tab.
🔍 SQL — Structured Query Language 9.1.4
SQL (say "es-queue-el") is the standard language for getting useful information out of a database: a visitor only needs a patient's ward and bed; a consultant needs a list of all their patients. An SQL script is a list of SQL commands that do a task, often saved in a file so it can be reused. You need to understand and predict the output of these statements:
| Statement | What it does | Form |
|---|---|---|
| SELECT | Fetches the fields (columns) to show — every query begins with it | SELECT Field1, Field2 · SELECT * |
| FROM | Identifies the table | FROM PATIENT |
| WHERE | Includes only the records (rows) that match a condition | WHERE WardNumber = 6 |
| ORDER BY | Sorts the results by a field, ascending (A→Z, 0→9) unless DESC | ORDER BY FamilyName · ORDER BY Price DESC |
| SUM | Adds up the values in a number field | SELECT SUM(Badges) |
| COUNT | Counts the records that match the condition | SELECT COUNT(*) |
Only SELECT and FROM are compulsory, and a command ends with ;. * means every field.
⚖️ Writing conditions 9.1.4
Values must match the field's data type
| Field type | Example value |
|---|---|
| text | 'Mr Smith' |
| character | 'M' |
| Boolean | TRUE |
| integer | 12 |
| real | 12.01 |
| date/time | '22/11/2022' |
Text, characters and dates go in single quotes. Access also accepts double quotes, and wants dates between hashes: #22/11/2022#.
Operators
| Operator | Meaning |
|---|---|
| = > < >= <= | equal, greater, less, greater or equal, less or equal |
| <> | not equal to |
| BETWEEN … AND … | within a range of two values (inclusive) |
| LIKE | matches a pattern: % any characters, _ one character (Access: * and ?) |
| IN (…) | matches any value in a list |
| AND / OR / NOT | all conditions true / at least one true / condition false |
🧪 SQL lab — with a 3D view of the table
Choose a table, write a query (or build one below) and press Run. In 3D, rows that fail WHERE sink back, fields not SELECTed fade, and ORDER BY restacks the matching rows. Drag to rotate.
🧱 Query builder
🛠️ Using a database for real 9.1
You need to be able to define a single-table database from given data storage requirements, choose a suitable primary key, and read, complete and understand SQL scripts. With a database management system (Access, or free ones such as SQLite and MySQL — check their commands, which can differ slightly):
- 1️⃣ Start from the data collection form: each box on the form becomes a field with a meaningful name.
- 2️⃣ Create a blank database, open the table in design view and give it a name (e.g. CUB).
- 3️⃣ Add each field with a suitable data type — plus an extra field to be the primary key if no existing field is unique.
- 4️⃣ Add validation to each field — e.g. Gender: a presence check, a length check (1 character) and a format check (M or F).
- 5️⃣ Enter the records, then use verification (a visual check against the form, or double entry) to make sure they were copied correctly.
🏕️ Case study: the cub scout database 9.1
Children aged seven to eleven can join a cub scout group, which keeps a record of each cub. The CUB table in the SQL lab has a primary key CubNumber, the cub's details, the Six they belong to (a group of up to six cubs, named Red, Blue, Yellow or Green) and the number of Badges they have been awarded.
🎓 A student database 9.1
A school's STUDENT table stores each student's family name, other names, student ID, date of birth, date of entry, class, year/grade and email address. It's in the SQL lab too — use it to test your scripts for Activity 9.8.
🚀 Extension: building a database with SQL A Level preview
A database management system (DBMS) uses a Data Definition Language (DDL) to create, modify and remove the structure of a database, and a Data Manipulation Language (DML) to add, modify, delete and retrieve the data itself. SQL does both — and has been the industry standard since the 1970s. DDL commands: CREATE DATABASE, CREATE TABLE, ALTER TABLE, PRIMARY KEY. SQL data types include CHAR(n) (fixed-length text), VARCHAR(n) (variable length, up to n), BOOLEAN, INTEGER, REAL, DATE (YYYY-MM-DD) and TIME (HH:MM:SS).
✏️ Practice hub 8 activities · every example
Every activity from the coursebook chapter — the SQL ones are marked by running your query's logic on the real tables. Fresh questions every time you hit 🔀 New questions.
📘 Worked examples
Every type of worked example in the chapter, step by step. Hit 🔀 Another example for a new one.
🃏 Key-term flashcards Tap to flip
🧠 Mini-quiz
📝 Exam-style questions Try, then peek
Questions 1–3 use the BIKE table in the SQL lab.
- ✅ 8 records (one per model) and 7 fields.
- ✅ ModelNumber — every model has a different model number, so it uniquely identifies each record. (FrameColour, WheelSize and the others all contain repeated values.)
- ✅ ModelNumber — text (letters and digits, never calculated) · Description, FrameColour — text · WheelSize — integer (whole number of mm) · Price — real (dollars and cents) · InProduction — Boolean (yes or no) · NumberInStock — integer.
- ✅ ModelNumber: presence, length (7) and format check (BY then 5 digits). Price: type and range check (greater than 0, e.g. up to 5000). NumberInStock: type (whole number) and range check (0 or more).
SELECT ModelNumber, Price FROM BIKE WHERE InProduction = TRUE ORDER BY Price DESC;
Run it in the SQL lab to see the six rows, from the Electric bike ($1299) down to the BMX ($239).
- ✅ SELECT names the fields to show — SELECT Description, Price (or * for all fields).
- ✅ FROM names the table — FROM BIKE.
- ✅ WHERE keeps only records matching a condition — WHERE WheelSize = 700.
- ✅ SUM adds up a number field over the matching records — SELECT SUM(NumberInStock) FROM BIKE gives the total bikes in stock.
- ✅ ORDER BY sorts the output — ORDER BY Price cheapest first, or ORDER BY Price DESC.
- ✅ Format check — SN followed by three digits; length check — exactly 5 characters (also a presence check, and it must be unique as the primary key).
SELECT Title FROM SHOWS WHERE Type = 'Comedy' AND SoldOut = FALSE;