← IGCSE Computer Science · Cambridge · GCSE Computer Science revision
⭐ 0
Cambridge IGCSE Computer Science (0478/0984) · Topic 9 Databases
Mini-Lesson

Databases

2 missions. Guess, learn, then check. Your place is saved.

  1. Mission 1Two students called Amy Khan: how does a database tell them apart?
  2. Mission 2How can one line of SQL find every overdue book in a library?
Mission 1

Two students called Amy Khan: how does a database tell them apart?

Make a guess: Two students called Amy Khan: how does a database tell them apart?

Databases · tables and keys

Tables, records and fields

Table, record, fieldtable: StudentfieldStudentIDFirstNameSurnameFormDateOfBirthS1041AmyKhan10B14/03/2011S1042LeoGrant10A02/09/2010S1057AmyKhan10C21/06/2011S1063PriyaShah10B30/11/2010record
  • A database is an organised, persistent store of data.
  • A table holds data about one kind of thing, such as students.
  • A record is one row: all the data about one student.
  • A field is one column: one item of data stored for every record, such as Surname.
  • Your databases have a single table: one table holds one set of data.
StudentIDFirstNameSurnameFormDateOfBirth
S1041AmyKhan10B14/03/2011
S1042LeoGrant10A02/09/2010
S1057AmyKhan10C21/06/2011
S1063PriyaShah10B30/11/2010
Calculate
?Look at the Student table on the previous screen. How many fields does each record have?
Databases · tables and keys

The primary key

  • A primary key is a field that holds a different value for every record, so it uniquely identifies each record.
  • Names, forms and even birthdays can repeat, so they cannot be keys.
  • Schools, shops and banks add an ID field, such as StudentID, to act as the key.
  • The database refuses a new record whose key value is already used.
Quick check
?A Car table has the fields Make, Model, Colour and Registration. Which field is the most suitable primary key?
Databases · tables and keys

Choosing data types

Field data typesField nameExampleData typeStudentIDS1041stringSiblings2integerHeight1.62realPaidTripTrueBooleanInitialAcharacter
  • Each field has a data type that fits the data it stores.
  • Integer for whole numbers, real for decimals, Boolean for TRUE/FALSE, character for one character, text/alphanumeric for text, date/time for dates and times.
  • Phone numbers and IDs are stored as text/alphanumeric: they are not used in calculations, can start with 0 and can contain letters or spaces.
  • When you define a table, add validation to fields, e.g. a range check on a Mark field or a presence check on Surname.
Quick check
?Which data type is most suitable for a field storing phone numbers such as 07700 900123?
Mission check

?Two students called Amy Khan: how does a database tell them apart?
Mission 2

How can one line of SQL find every overdue book in a library?

Make a guess: How can one line of SQL find every overdue book in a library?

Databases · SQL

SELECT and FROM

SELECT and FROMLoanFROM chooses the tableLoanIDTitleGenreDaysLateMemberIDL01HolesAdventure0M01L02WonderDrama3M02L03CoralineFantasy0M03L04SkelligFantasy7M04L05HolesAdventure2M05L06MatildaFantasy5M02SELECT chooses columnsResultTitleDaysLateHoles0Wonder3Coraline0Skellig7Holes2Matilda5
  • SQL (Structured Query Language) is used to search for data in a database.
  • SELECT lists the fields you want to see, separated by commas.
  • FROM names the table to search.
  • This query shows the Title and DaysLate of every record in Loan:
SQL
SELECT Title, DaysLate
FROM Loan
LoanIDTitleGenreDaysLateMemberID
L01HolesAdventure0M01
L02WonderDrama3M02
L03CoralineFantasy0M03
L04SkelligFantasy7M04
L05HolesAdventure2M05
L06MatildaFantasy5M02
Databases · SQL

WHERE picks the records

WHERE keeps matching rowsLoanWHERE Genre = 'Fantasy'LoanIDTitleGenreDaysLateMemberIDL01HolesAdventure0M01L02WonderDrama3M02L03CoralineFantasy0M03L04SkelligFantasy7M04L05HolesAdventure2M05L06MatildaFantasy5M02WHERE keeps matching rowsResultTitleDaysLateCoraline0Skellig7Matilda5
  • WHERE gives a condition. Only records that meet it are returned.
  • Compare with = < > <= >= and <> (not equal).
  • Put text values in quotes: Genre = 'Fantasy'. Numbers need no quotes.
  • Join conditions with AND (both must be true) or OR (either can be true): WHERE Genre = 'Fantasy' AND DaysLate > 0 returns Skellig and Matilda.
SQL
SELECT Title, DaysLate
FROM Loan
WHERE Genre = 'Fantasy'

Result: Coraline 0, Skellig 7, Matilda 5.

Your answer
?Use the Loan table. What does this query return? Give the values in order, separated by commas.
SQL
SELECT LoanID
FROM Loan
WHERE DaysLate >= 5
Databases · SQL

Writing a query

  • Read the question for the fields to show (SELECT), the table (FROM) and the rule (WHERE).
  • Fields in WHERE do not have to appear in SELECT.
  • ORDER BY sorts the results by a field: ASC (ascending, A to Z, smallest first) or DESC (descending, largest first).
  • SUM(field) adds up a numeric field; COUNT(field) counts the records returned.
SQL
SELECT Title, DaysLate
FROM Loan
WHERE Genre = 'Fantasy'
ORDER BY DaysLate DESC

SELECT SUM(DaysLate)
FROM Loan
WHERE Genre = 'Fantasy'

The first returns Skellig 7, Matilda 5, Coraline 0. The second returns 12.

Calculate
?Use the Loan table. What value does this query return?
SQL
SELECT SUM(DaysLate)
FROM Loan
WHERE Genre = 'Drama' OR Genre = 'Fantasy'
Mission check

?How can one line of SQL find every overdue book in a library?
Recap

Big ideas

  • A table stores records (rows); each record has the same fields (columns), each with a suitable data type.
  • A primary key uniquely identifies each record.
  • SELECT fields FROM table WHERE condition: the fields to show, the table to search and the records to keep.
  • ORDER BY … ASC or DESC sorts the results.
  • SUM adds up and COUNT counts; AND and OR combine conditions.
🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Databases for Cambridge IGCSE Computer Science (0478/0984). 🎉

  1. Mission 1Two students called Amy Khan: how does a database tell them apart?
  2. Mission 2How can one line of SQL find every overdue book in a library?

Your stars: 0 / 0

Next: test yourself in the Verify stage.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to IGCSE Computer Science · Cambridge

Specification points and sources for this lesson are listed at the bottom of the page.

Sources (2)

The specification points and sources behind this lesson.

  1. 1 Cambridge 0478 syllabus 2026–2028, 9.1–9.3
  2. 2 Cambridge 0478 syllabus 2026–2028, 9.4