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.
A relational database stores data in several tables that are linked together.
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, string for text.
Phone numbers and IDs are stored as strings: they are not used in calculations, can start with 0 and can contain letters or spaces.
Quick check
?Which data type is most suitable for a field storing phone numbers such as 07700 900123?
Databases · tables and keys
Linking tables with a foreign key
A foreign key is a field in one table that is the primary key of another table. It links the two tables.
FormID is the primary key of Form, and a foreign key in Student.
Each tutor's name is stored once, in Form, not repeated in every student record. This removes data redundancy (needless repeats).
If a tutor changes, only one record is edited, so copies can't disagree: no data inconsistency.
Quick check
?Student(StudentID, FirstName, FormID) and Form(FormID, Tutor, Room) are linked. Which field is the foreign key?
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.
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).
To use two tables, list both after FROM and match the linking key in WHERE, writing Table.Field.
SQL
SELECT Title, DaysLate
FROM Loan
WHERE Genre = 'Fantasy'
ORDER BY DaysLate DESC
SELECT Member.Name, Loan.Title
FROM Member, Loan
WHERE Member.MemberID = Loan.MemberID
The first returns Skellig 7, Matilda 5, Coraline 0. The second gives each loan's title with the borrower's name from a Member(MemberID, Name, Form) table.
Quick check
?Use the Loan table. Which title is listed first by this query?
SQL
SELECT Title
FROM Loan
WHERE DaysLate > 0
ORDER BY DaysLate DESC
Databases · SQL
Adding, changing and deleting data
INSERT INTO adds a new record: list the fields, then the VALUES in the same order.
UPDATE changes values: SET gives the new values, WHERE picks the records.
DELETE FROM removes the records that match WHERE.
Leave out WHERE and UPDATE or DELETE affects every record.
SQL
INSERT INTO Loan (LoanID, Title, Genre, DaysLate, MemberID)
VALUES ('L07', 'Holes', 'Adventure', 0, 'M03')
UPDATE Loan
SET DaysLate = 0
WHERE LoanID = 'L02'
DELETE FROM Loan
WHERE LoanID = 'L07'
Calculate
?Start from the original Loan table (6 records). These two statements run in order. How many records are left?
SQL
UPDATE Loan
SET DaysLate = 0
WHERE MemberID = 'M02'
DELETE FROM Loan
WHERE DaysLate = 0
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.
A foreign key links tables in a relational database, cutting data redundancy and inconsistency.
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.
INSERT INTO adds records, UPDATE … SET changes them and DELETE FROM removes them; WHERE decides which.
🏆
Mini-lesson complete!
⭐⭐⭐
You've worked through Relational databases and SQL for AQA GCSE Computer Science (8525). 🎉
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?