A-level Computer Science revision · Exchanging data

Relational databases and keys

The idea

A relational database stores data in tables (relations) linked by keys. A primary key uniquely identifies each record. A foreign key is a field (or fields) that refers to the primary key of another table, creating a relationship. Relationships are one-to-one, one-to-many or many-to-many; many-to-many needs a link table. Secondary keys are indexed for fast searching.

  1. Choose a unique primary key.
  2. Link tables with a foreign key.
  3. Resolve many-to-many with a link table.

Watch out A many-to-many relationship cannot be implemented directly; it needs a linking table.

See it worked

Worked example

Students take many courses, and courses have many students. How is this modelled?

  1. It is a many-to-many relationship.
  2. Create a link table, Enrolment.
  3. It holds StudentID and CourseID as foreign keys.

Answer With a link table holding both foreign keys

Your turn

Question 11 mark

What is a foreign key?

Choose one
Question 21 mark

A customer can place many orders, but each order belongs to one customer. This is:

Choose one
Question 32 marks

Why are secondary keys indexed?

Write your answer, then see a model answer
  1. Define the index.
  2. State the benefit.

Answer An index lets the database find records by that field quickly without searching every record, speeding up common queries.

A-level Computer Science Pro, OCR H446

A-level Computer Science Pro

£119 once

Price rises to £199 on 1 January. Save £80 by joining before then.

At least 12 months of access

By paying you agree to our Terms and get access straight away, which ends your 14-day cancellation right.

No VAT is charged.

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms