GCSE · Computer Science · AQA · Spec 8525
Foreign key
Six orders sit in a pile. None has a name on it, just a number. Whose is whose? Do the database's job by hand and foreign keys click.
Databases · Foreign keys
File the orders
Pick an order, then pick the customer whose primary key equals the customer ID stored in that order.
Still to sort
101 · Asha (0)
Customers record with primary key 101
102 · Ben (0)
Customers record with primary key 102
103 · Chloe (0)
Customers record with primary key 103
You're the database now. Each order only stores a customer ID, so match that number to a customer's primary key.
Computer Science · Structure
Two tables, one link
Tap a table to see its keys. The line is the match you just did by hand.
Tap any part of the diagram to see what it does.
Predict, then check
Order 9001 sits in the Orders table. The Customers table only has customer IDs 101, 102 and 103.
Order 9001 stores customer ID 999. No record in Customers has 999. What can the database say about who placed it?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- A relational database keeps data in more than one table, and links the tables through shared fields.
- Every table has a primary key: a field whose value uniquely identifies each record.
- A foreign key is a field in one table that holds values of another table's primary key.
Have a goYour classmate insists: 'Customer ID in the Orders table is a primary key. It's got ID in the name!' What do you tell them?
It's a foreign key: it holds values of the Customers table's primary key.
What a field holds decides what it is, not its name. A foreign key holds another table's primary key values.
- To follow the link, find the record in the other table whose primary key value equals your foreign key value.
- A foreign key doesn't have to be unique in its own table. Many orders can carry the same customer ID.
- When many records refer to one record like that, it's called a one-to-many relationship.
Have a goIn an Orders table, customer ID 2 appears in four records and customer ID 5 in one. How many different customers do these five orders belong to?
Two customers.
Repeated foreign key values still point at the same record, so five orders can refer to just two customers.
- A foreign key value should match a primary key value that exists in the linked table.
- If it refers to a record that doesn't exist, the data is left inconsistent.
- Store data about each item once, refer to it by key, and you reduce data redundancy.
Have a goAsha has placed 40 orders. How many copies of her address are stored if every order repeats it? And if it lives only in a Customers table?
40 copies, against 1.
Linked tables store data about each item once and refer to it by key, which reduces data redundancy.
The big picture
A foreign key is a field in one table that holds values of another table's primary key. Matching the two values links a record to the one it refers to. Unlike a primary key, a foreign key can repeat, which gives a one-to-many relationship. Every foreign key value should match an existing primary key, and storing data once and referring to it by key reduces redundancy.
Key points
Worked example
Problem
A library has a Members table (member ID is the primary key): 21 Jo, 22 Kai, 23 Lee. Its Loans table (loan ID is the primary key, member ID is a foreign key) holds: loan 901 → member 22, loan 902 → member 21, loan 903 → member 22, loan 904 → member 22. Which member took out loan 903, how many loans does Kai have, and does every loan refer to a member who exists?
⚠ Watch out
Believing a foreign key must be unique, or that customer ID in Orders is Orders' own primary key. In its own table a foreign key value can repeat. It's the primary key that never does.
Memory hook
A foreign key is a borrowed ID: it belongs to another table's primary key, and it can be borrowed as often as you like.
Check yourself
A Pets table stores an owner ID in every pet record, taken from the Owners table. Two pets hold the same owner ID. Is that a problem? Say why in one sentence.
Flashcards
(11)What is a relational database?
What is a primary key?
What is a foreign key?
How do you find the record a foreign key refers to?
Can a foreign key value repeat within its own table?
What is a one-to-many relationship?
In a one-to-many link, which table holds the foreign key?
Why should every foreign key value match a primary key value?
What happens if a foreign key refers to a record that doesn't exist?
What is data redundancy?
How do foreign keys reduce data redundancy?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 9 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.