Syllabus coverage
- Define a single-table database from given data storage requirements, including fields, records and validation
- Suggest suitable basic data types, including text/alphanumeric, character, Boolean, integer, real and date/time
- Understand the purpose of a primary key and identify a suitable primary key for a given database table
- Read, understand and complete structured query language (SQL) scripts to query data stored in a single database table
- SQL is limited to SELECT, FROM, WHERE, ORDER BY DESCENDING, ORDER BY ASCENDING, SUM, COUNT, AND and OR
- Identify the output given by an SQL statement that will query the given contents of a database table
Part of the IGCSE Computer Science syllabus 0478.
What a single-table database is
A database is a collection of data that is organised so it can be searched, sorted and updated quickly. A single-table database holds all of its data in one table: a grid in which each field is a column and each record is a row. Every record describes one thing - one product, one pupil, one booking - and every record has a value in every field, so the table keeps the same shape all the way down.
Take a stock table for a shop. Its fields might be ProductCode, ProductName, Price, QuantityInStock, InStock and LastOrdered. Each record is one product: P101, Kettle, 24.50, 12, TRUE, 12/03/2026. Nothing is stored twice, and every record follows the same pattern, which is exactly what makes the table searchable and sortable.
A field is one item of data about the thing the record describes, and it is defined once for the whole table: the field Price is a column, and it holds the price of every product. A record is one complete row, made up of one value from each field. The design of the table - the set of fields, with a chosen data type for each - is settled before any data is entered.
| Term | What it means | In the stock table |
|---|---|---|
| Field | One item of data about the thing a record describes. It is a column of the table and is defined once for every record. | ProductCode, ProductName, Price, QuantityInStock |
| Record | One complete row of the table, holding one value in every field, describing one thing. | P101, Kettle, 24.50, 12 |
| Table | The whole grid of fields and records. A single-table database stores all of its data here and nowhere else. | Every product the shop sells |
| Data storage requirement | The description of what data must be stored, which you turn into a list of fields with a data type for each. | 'we need each product's code, name, price and how many we have' |
Designing the table from a data storage requirement
A question that asks you to define a single-table database from given data storage requirements gives you a description of what has to be stored and expects a list of fields. Work through the description and write down one field for each separate item of data it mentions. If the requirement says 'the library must record each book's title, author, the date it was borrowed and whether it has been returned', that is four fields: Title, Author, DateBorrowed and Returned.
Each field holds one item of data. Do not put two things in one field: a field called NameAndAddress cannot be sorted by surname and cannot be searched by town. Split it into separate fields - Surname, FirstName, Street, Town, Postcode - and the table becomes far more useful.
Field names are single words with no spaces, written so that anyone can tell what they hold, such as DateBorrowed. The order of the fields makes no difference to the database, but it makes a difference to you: a sensible order makes the table much easier to read when you are working out the output of a query by hand.
Finally, decide for each field whether a value is required. A field that every record must have cannot be left empty, and that is where validation does its work.
- One field for every separate item of data the storage requirement names.
- One item of data per field: split a combined field such as NameAndAddress into separate fields.
- Field names are single words with no spaces, chosen so the meaning is obvious.
- The set of fields, with a data type for each, is the design of the table - settle it before any data is entered.
Choosing a suitable data type
Every field has a data type, which decides what kind of value it can hold and what can be done with it. The syllabus names six: text/alphanumeric, character, Boolean, integer, real and date/time. Choosing the wrong one is the commonest way to lose marks on this topic, so decide from the data you are actually storing.
Text/alphanumeric holds any combination of letters, digits and symbols and is never used in arithmetic: a product name, an address, a postcode. Character holds exactly one character: a single letter grade, a one-letter discount code, Y or N. Boolean holds one of two values, TRUE or FALSE, and nothing else: whether an item is in stock, whether a book has been returned.
Integer holds whole numbers only: a quantity in stock, a number of pupils, a year. Real holds numbers with a fractional part, so it is used for anything measured or priced: a price of 24.50, a weight of 1.5, a temperature. Date/time holds a date, a time, or both, kept in a fixed format so the database can put records in date order and work out how long ago something happened.
Two choices catch people out. A price is a real, not an integer, because 24.50 has a fractional part and an integer field would lose the 50p. A telephone number is text/alphanumeric, not an integer, because it is a string of digits that is never added up and it may begin with a zero, which an integer would drop.
| Data type | What it holds | Suitable for |
|---|---|---|
| text/alphanumeric | Any letters, digits and symbols, in any combination | ProductName, Address, TelephoneNumber |
| character | Exactly one character | DiscountCode A, Grade B, a Y/N flag |
| Boolean | One of two values: TRUE or FALSE | InStock, Returned, HasPaid |
| integer | A whole number, positive or negative | QuantityInStock, NumberOfPupils, YearOfBirth |
| real | A number with a fractional part | Price, Weight, Temperature |
| date/time | A date, a time, or both | DateBorrowed, TimeOfSale, LastOrdered |
The primary key
Every record in a table has to be findable, and that is the job of the primary key. A primary key is a field that uniquely identifies each record: no two records may hold the same value in that field. In the stock table, ProductCode is the primary key, because every product has its own code and no two products share one.
The primary key must also be present in every record - it can never be left empty - and it must not change, because it is how the database refers to that record. A value that is unique today but might be edited tomorrow makes a poor primary key.
Identifying a suitable primary key means testing a candidate field against those rules. ProductName fails, because two different products can share a name: a shop may sell two products both called Kettle. Price fails. DateBorrowed fails, because two books borrowed on the same day would clash. If the table has no field that is naturally unique, the answer is to add one, such as a PupilID or a booking reference, and use that.
Without a primary key a table can hold duplicate records, and the database has no reliable way to pick out one record from all the others. That is the purpose of the key: uniqueness, and the ability to identify a record.
- A primary key uniquely identifies each record: no two records may share the same value in that field.
- It must have a value in every record, and that value must not change.
- ProductCode or PupilID works because the values are unique; ProductName, Price and DateBorrowed do not, because two records could share them.
- If no field is naturally unique, add one and make that the primary key.
Validation when the data is entered
Defining a single-table database also means deciding what counts as acceptable data. Validation is the automatic check made as data is entered, to make sure it is sensible before it is stored. It does not make the data correct - it only rejects data that breaks a rule - but it stops obvious errors before they reach the table.
The syllabus names the checks you need. A range check requires the value to lie between limits, such as a quantity between 0 and 500. A length check requires a set number of characters, such as a product code of exactly four. A type check requires the right data type, so no letters in a quantity field. A presence check requires the field not to be left empty. A format check requires the value to follow a pattern, such as a date written as DD/MM/YYYY. A check digit is an extra digit calculated from the others and checked when the value is entered, which catches typing errors in a long number such as a barcode.
Validation is worth marks as an example, not just as a name. 'A range check stops a quantity of 700 being stored' scores; 'a range check' on its own often does not. Say which field the check is applied to and what it would reject.
| Validation check | What it does | Example for the stock table |
|---|---|---|
| Range check | The value must lie between a lowest and a highest acceptable value | QuantityInStock must be between 0 and 500 |
| Length check | The value must have a set number of characters | ProductCode must be exactly 4 characters |
| Type check | The value must be of the right data type | QuantityInStock must be a whole number, not the word twelve |
| Presence check | The field must contain something | ProductName cannot be left blank |
| Format check | The value must match a pattern | LastOrdered must be in the form DD/MM/YYYY |
| Check digit | An extra digit calculated from the others and checked on entry | The final digit of a 13-digit barcode |
Reading a structured query: the SQL clauses
A structured query language (SQL) statement asks a question of the data in a table. The syllabus limits you to a single table and to nine keywords, so the statements you meet are short - but you must know exactly what each part does, because questions ask you to read a script, to complete a script, and to say what a script will output.
SELECT names the fields you want in the answer. FROM names the table they come from. WHERE decides which records are returned: only the records for which the condition is true are included, and the rest are left out altogether. ORDER BY ... ASCENDING and ORDER BY ... DESCENDING put the records that survived WHERE into order by one field, smallest or first in the alphabet first, or largest or last first.
AND and OR join two conditions inside a WHERE clause. A record is returned only if both conditions are true when AND is used, and if either condition is true when OR is used. SUM adds up the values in a numeric field and COUNT counts how many records were selected; both return a single number instead of a list of records.
Read a statement outwards from the keywords: FROM tells you the table, WHERE tells you which records, SELECT tells you which fields of those records are shown, and ORDER BY tells you the sequence they appear in. Working through those four steps in writing is how you avoid a wrong answer when the output has several records. Cambridge writes the sort direction in full, as ASCENDING or DESCENDING; some database packages accept the short forms ASC and DESC, but write the full words in the exam.
| Keyword | What it does | Example |
|---|---|---|
| SELECT | Names the fields to show in the output; * means every field | SELECT ProductName, Price |
| FROM | Names the table the data comes from | FROM Stock |
| WHERE | Keeps only the records that satisfy the condition | WHERE Price > 20.00 |
| AND | Both conditions must be true for the record to be returned | WHERE Price > 20.00 AND QuantityInStock > 0 |
| OR | Either condition may be true for the record to be returned | WHERE QuantityInStock = 0 OR Price < 5.00 |
| ORDER BY ... ASCENDING | Sorts the records by a field, smallest or first in the alphabet first | ORDER BY Price ASCENDING |
| ORDER BY ... DESCENDING | Sorts the records by a field, largest or last in the alphabet first | ORDER BY Price DESCENDING |
| SUM | Adds up the values of a numeric field across the selected records | SELECT SUM(QuantityInStock) |
| COUNT | Counts how many records were selected | SELECT COUNT(ProductCode) |
Working out the output of a query
Take a table and a statement, and work it out on paper in four steps. The Stock table below holds four records, and the statement is: SELECT ProductName, Price FROM Stock WHERE Price > 20.00 AND QuantityInStock > 0 ORDER BY Price DESCENDING.
Step 1, the table. FROM Stock, so all four records are in play. Step 2, the records. WHERE asks for records whose price is greater than 20.00 and whose quantity is greater than 0. P101 Kettle at 24.50 with 12 in stock passes both tests. P104 Kettle at 31.00 with 4 in stock passes both. P103 Blender at 39.99 has a quantity of 0, so the AND fails and it is out. P102 Toaster at 18.00 fails the price test, so it is out as well.
Step 3, the fields. SELECT ProductName, Price, so each surviving record is shown as its name and its price only - the code and the quantity are not printed. Step 4, the order. ORDER BY Price DESCENDING, so the largest price comes first. The output is two records: Kettle, 31.00 and then Kettle, 24.50.
SUM and COUNT are worked out the same way and then reduced to a single number. SELECT COUNT(ProductCode) FROM Stock WHERE QuantityInStock > 0 counts the records that pass the condition, which is 3 here: P101, P102 and P104. SELECT SUM(QuantityInStock) FROM Stock adds the quantity field across every record: 12 + 6 + 0 + 4 = 22.
| ProductCode | ProductName | Price | QuantityInStock |
|---|---|---|---|
| P101 | Kettle | 24.50 | 12 |
| P102 | Toaster | 18.00 | 6 |
| P103 | Blender | 39.99 | 0 |
| P104 | Kettle | 31.00 | 4 |
Key terms you must be able to define
- Single-table database
- A database in which all of the data is held in one table, with no links to any other table.
- Field
- One item of data about the thing a record describes. A field is a column of the table, and it is defined once for every record.
- Record
- One complete row of a table, made up of one value from each field, describing one thing such as one product or one pupil.
- Data type
- The kind of value a field may hold: text/alphanumeric, character, Boolean, integer, real or date/time.
- Primary key
- A field that uniquely identifies each record in a table. No two records may share the same value, the value must be present in every record, and it must not change.
- Validation
- An automatic check made as data is entered, to make sure it is sensible before it is stored. It cannot tell whether the data is actually correct.
- Range check
- A validation check that the value lies between a lowest and a highest acceptable value, for example a quantity between 0 and 500.
- Length check
- A validation check that the value has a set number of characters, for example a product code of exactly four characters.
- Type check
- A validation check that the value is of the right data type, for example no letters in a field that must hold a whole number.
- Presence check
- A validation check that the field has not been left empty.
- Format check
- A validation check that the value follows a pattern, for example a date written as DD/MM/YYYY.
- Check digit
- An extra digit calculated from the other digits of a number and checked when the number is entered, used to catch typing errors.
- Structured query language (SQL)
- The language used to ask questions of the data in a database. On this syllabus the statements are limited to a single table.
- SELECT
- The SQL keyword that names the fields to be shown in the output of the query.
- FROM
- The SQL keyword that names the table the data is taken from.
- WHERE
- The SQL keyword that decides which records are returned: only the records for which the condition is true.
- ORDER BY
- The SQL keyword that sorts the selected records by a field, either ASCENDING or DESCENDING.
- SUM
- The SQL function that adds up the values of a numeric field across the selected records and returns one number.
- COUNT
- The SQL function that counts how many records were selected and returns one number.
Applied to real systems
Cambridge sets 0478 in systems you already use - a phone, a school network, a supermarket till, a website, a bank. Use these as models for how to apply the theory to a context you have not seen before, then write your own.
The stock table uses ProductCode as its primary key, because every product has its own code and no two products share one. Price is a real field so that 24.50 is stored exactly, QuantityInStock is an integer because it is a whole number of items, and InStock is Boolean because the answer is only ever TRUE or FALSE. A range check on QuantityInStock stops a mistyped 700 being stored as if the shop had seven hundred kettles.
A pupil table has PupilID as its primary key, with Surname, FirstName, DateOfBirth, YearGroup and HasPaid. Surname is text/alphanumeric because it is only ever read and searched, never calculated with; DateOfBirth is date/time so the records can be sorted into age order; HasPaid is Boolean. A query such as SELECT COUNT(PupilID) FROM Pupil WHERE YearGroup = 10 AND HasPaid = TRUE answers 'how many Year 10 pupils have paid?' in one line.
One table holds every booking, with BookingReference as the primary key, ShowingTime as date/time, SeatNumber as text/alphanumeric and Paid as Boolean. To find the takings for a showing, the manager queries SELECT SUM(TicketPrice) FROM Booking WHERE ShowingTime = 19:30 - the SUM adds a real field across the selected records and returns a single number. BookingReference has to be unique, because two customers in different seats at the same showing must not share a reference.
MemberID is the primary key, DateJoined is date/time, MembershipFee is real and Active is Boolean. ORDER BY DateJoined ASCENDING lists the members who joined first, which is what the gym needs when it is deciding who gets the ten-year discount. A format check on DateJoined rejects a date typed as 3/3/26 when the table expects DD/MM/YYYY, and a presence check stops a record being saved with no surname.
The appointments table holds AppointmentID as the primary key, PatientName and Clinic as text/alphanumeric, AppointmentDate as date/time and Attended as Boolean. SELECT COUNT(AppointmentID) FROM Appointment WHERE Attended = FALSE gives the number of patients who did not turn up, and a length check on AppointmentID rejects any reference that is not the expected number of characters before the record is saved.
Flashcards
Click a card to flip it. Mark honestly — the tracker uses this.
Exam-style questions
7 questions, 23 marks. Write your answer, then reveal the mark scheme and tick each point you actually made. Answering honestly here is what makes the rest of the site useful.
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Exam technique for this topic
- Choose the data type from the data itself and say why in the same breath: price -> real, because it has a fractional part; quantity -> integer, because it is a whole number; has the book been returned? -> Boolean, because there are only two possible values.
- When you are asked to identify a primary key, name the field and then give the reason in one sentence: the value is unique to each record, so no two records can be confused. Never offer a field such as Name, Price or Date, because two records can share those values - if nothing in the table is unique, add a new field such as a code or an ID and use that.
- For an output question, work through the statement in the order FROM, WHERE, SELECT, ORDER BY, and write down which records survive WHERE before you think about anything else. Then apply the order to the records you have left.
- Write the SQL the way the syllabus does: ORDER BY Price DESCENDING, with the keyword in full. A correct query with a made-up keyword loses the mark.
- Validation answers want an example attached: 'a range check on QuantityInStock, so a quantity of 700 is rejected' rather than just 'a range check'.