← O Level

CS · Topic 9

Section 2 · Chapter 9 · IGCSE & O Level Computer Science

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.

🕶️ with Guru Jazzy

🗄️ What is a database? 9.1.1

Database — a persistent, structured collection of data that lets people extract information in a way that meets their needs. It can hold text, numbers, pictures — anything a computer can store.

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

Table — a collection of related records about one type of item, person or event, with a meaningful name such as PATIENT, BOOK or APPOINTMENT.
Record — a row: all the data about one item, person or event (one patient).
Field — a column: one piece of information stored for every record (every patient's WardNumber). Field names are a single word with no spaces, e.g. BedNumber.

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 typeHoldsAccess data typeExample field
text / alphanumericA number of charactersShort Text / Long TextFamilyName
characterA single characterShort Text, field size 1Gender
BooleanOne of two values: True/False, 1/0, Yes/NoYes/NoInProduction
integerA whole numberNumber, fixed, 0 decimal placesWardNumber
realA decimal numberNumber, decimalPrice
date/timeA date and/or a timeDate/TimeDateOfAdmission
Watch out — a telephone number or a code like BY00007 is text, not integer: it may start with 0 or contain letters, and you never do maths with it.

🔑 Primary keys 9.1.3

Primary key — a field that uniquely identifies each record. Its values are never repeated in the table.

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:

StatementWhat it doesForm
SELECTFetches the fields (columns) to show — every query begins with itSELECT Field1, Field2 · SELECT *
FROMIdentifies the tableFROM PATIENT
WHEREIncludes only the records (rows) that match a conditionWHERE WardNumber = 6
ORDER BYSorts the results by a field, ascending (A→Z, 0→9) unless DESCORDER BY FamilyName · ORDER BY Price DESC
SUMAdds up the values in a number fieldSELECT SUM(Badges)
COUNTCounts the records that match the conditionSELECT 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 typeExample value
text'Mr Smith'
character'M'
BooleanTRUE
integer12
real12.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

OperatorMeaning
= > < >= <=equal, greater, less, greater or equal, less or equal
<>not equal to
BETWEEN … AND …within a range of two values (inclusive)
LIKEmatches a pattern: % any characters, _ one character (Access: * and ?)
IN (…)matches any value in a list
AND / OR / NOTall 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

SELECT
or
WHERE
ORDER BY

🛠️ 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. 1️⃣ Start from the data collection form: each box on the form becomes a field with a meaningful name.
  2. 2️⃣ Create a blank database, open the table in design view and give it a name (e.g. CUB).
  3. 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. 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. 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;
📌 Chapter summary — A database is a persistent, structured collection of data; a single-table database has one table. A table holds records (rows) about one kind of thing; each record has the same fields (columns). Each field has a data type — text/alphanumeric, character, Boolean, integer, real, date/time — and validation rules. A primary key uniquely identifies each record. SQL queries a database: SELECT fields FROM table WHERE condition ORDER BY field, with SUM and COUNT to total and count.
Guru Jazzy 🕶️ · Chapter 9 · Databases · SELECT * FROM knowledge; 🗄️