← GCSE Computer Science · AQA · GCSE Computer Science revision
⭐ 0
AQA GCSE Computer Science (8525) · 3.7 Relational databases and SQL
Mini-Lesson

Relational databases and SQL

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.
  • A relational database stores data in several tables that are linked together.
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, 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

Foreign key linkStudentforeign keyStudentIDFirstNameSurnameFormIDS1041AmyKhan10BS1042LeoGrant10AS1057AmyKhan10CS1063PriyaShah10BFormFormIDTutorRoom10AMr HallR1210BMs OseiR1410CDr PatelR3underlined= primary keyforeign 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?
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.
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, UPDATE, DELETEINSERT INTOadds L07LoanIDTitleGenreDaysLateMemberIDL06MatildaFantasy5M02L07HolesAdventure0M03UPDATEL02 DaysLate: 3 becomes 0LoanIDTitleGenreDaysLateMemberIDL01HolesAdventure0M01L02WonderDrama0M02DELETE FROMremoves L07LoanIDTitleGenreDaysLateMemberIDL06MatildaFantasy5M02L07HolesAdventure0M03
  • 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?
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). 🎉

  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 GCSE Computer Science · AQA

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 AQA 8525 spec v1.3, 3.7.1 (and 3.2.1 data types)
  2. 2 AQA 8525 spec v1.3, 3.7.2