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

6 of 6 still to sort.

You're the database now. Each order only stores a customer ID, so match that number to a customer's primary key.

Watch out: Every order holds one customer ID, so it can only sit in one pile. But a pile can grow as big as it likes.

Computer Science · Structure

Two tables, one link

Tap a table to see its keys. The line is the match you just did by hand.

customer ID = customer ID

Tap any part of the diagram to see what it does.

Databases · Keys

Same name, different job

For each field, tick every statement that is true of it.

customer ID in Customers
customer ID in Orders
order ID in Orders

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?

Flat table or linked tables?

One flat tablevsTwo linked tables

Asha has placed 3 orders. Where do her details live?

Focus

Times Asha's details are stored

One flat table

3 — copied onto every one of her orders

Two linked tables

1 — in her Customers record

The insight

Storing the same data more than once is data redundancy. Linking cuts it.

What each order holds about its customer

One flat table

A full copy of the customer's details

Two linked tables

Just a customer ID — the foreign key

How an order finds its customer's details

One flat table

They're already on the same row

Two linked tables

Match its customer ID to the primary key in Customers

Databases · Write it up

Say it in your own words

Explain how a foreign key links a record in one table to a record in another table, and why the same foreign key value can appear in many records. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1A foreign key is a field that holds another table's primary key values.
2Matching equal values links each record to the one record it refers to.
3Primary key values are unique in their own table; foreign key values can repeat.
4Each foreign key value should match a primary key value that exists.
5Linked tables hold data once, so there is less redundancy.

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?
A database that stores data in more than one table, with the tables linked to each other through shared fields.
What is a primary key?
A field whose value uniquely identifies each record in a table.
What is a foreign key?
A field in one table that holds values of the primary key of another table.
How do you find the record a foreign key refers to?
Look in the other table for the record whose primary key value equals the foreign key value.
Can a foreign key value repeat within its own table?
Yes. The same value can appear in many records, for example many orders with the same customer ID. A primary key value can't repeat.
What is a one-to-many relationship?
One record in a table is referred to by many records in another, shown by one foreign key value appearing in many records. One customer, many orders.
In a one-to-many link, which table holds the foreign key?
The 'many' side. Orders holds customer ID as the foreign key, because many orders can refer to one customer.
Why should every foreign key value match a primary key value?
So it refers to a record that really exists in the linked table.
What happens if a foreign key refers to a record that doesn't exist?
The link points at nothing and the data is left inconsistent.
What is data redundancy?
The same data being stored more than once.
How do foreign keys reduce data redundancy?
Data about each item is stored once in its own table and referred to elsewhere by its key.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.