2 missions. Guess, learn, then check. Your place is saved.
Mission 1Two students called Amy Khan: how does a database tell them apart?
Mission 2How can one line of SQL find every overdue book in a library?
The specification points and sources are listed under the lesson: see Sources.
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
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.
StudentID
FirstName
Surname
Form
DateOfBirth
S1041
Amy
Khan
10B
14/03/2011
S1042
Leo
Grant
10A
02/09/2010
S1057
Amy
Khan
10C
21/06/2011
S1063
Priya
Shah
10B
30/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
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?
Each record has a primary key field, such as StudentID, whose value is different in every record. The two Amy Khans have different StudentIDs (S1041 and S1057), so the database changes exactly the right record.
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
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
LoanID
Title
Genre
DaysLate
MemberID
L01
Holes
Adventure
0
M01
L02
Wonder
Drama
3
M02
L03
Coraline
Fantasy
0
M03
L04
Skellig
Fantasy
7
M04
L05
Holes
Adventure
2
M05
L06
Matilda
Fantasy
5
M02
Databases · SQL
WHERE picks the records
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?
SELECT Title FROM Loan WHERE DaysLate > 0. SELECT chooses the fields to show, FROM names the table, and WHERE keeps only the records that meet the condition, however many records the table holds.
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). 🎉
Mission 1Two students called Amy Khan: how does a database tell them apart?
Mission 2How can one line of SQL find every overdue book in a library?