Higher Computing Science
Compound keys in databases
Higher Computing Science: Database design and development
A compound key is a primary key made of two (or more) fields together. The examiners' reports for 2023 and 2024 say the same thing: explanations need two points, and they must use the fields in the question.
An example
A swimming gala records each swimmer's time in each race:
| raceID | swimmerID | time |
|---|---|---|
| R1 | S04 | 31.2 |
| R1 | S09 | 29.8 |
| R2 | S04 | 65.0 |
| R3 | S09 | 58.4 |
R1 appears twice, because a race has many swimmers. S04 appears twice, because a swimmer enters many races. Neither field identifies a result on its own. Together, (raceID, swimmerID) does: a swimmer has one time in each race.
Why it is there
Result sits between Race and Swimmer. One race has many results, and one swimmer has many results: that is how a many-to-many relationship between Race and Swimmer is stored. Each part of the compound key is also a foreign key, linking to its own table.
- Unique: the combination of raceID and swimmerID is different for every record, although each one on its own repeats.
- Never blank: no part of the compound key can be left empty, so every result must have both a race and a swimmer.
- "It is two fields." True, but not an explanation.
- "It makes the table more secure." Keys are about identifying records, not security.
A table can also use a single new field instead, such as a resultID. If a question asks why a table uses one, the answer is usually that the same pair of values can legitimately happen twice, such as the same customer booking the same performance twice.
Try two exam-style questions
Choose an answer, say how sure you are, then press Check. Every wrong answer tells you why it is wrong.
The full course covers every topic like this. Every topic in Higher Computing Science has a short note, and a practice quiz with feedback on every answer, plus coding lessons in Python, SQL and HTML, a full mock exam and a progress report for parents.
More guides
This site is independent: it is not connected to the SQA or Qualifications Scotland. Points from the examiners' course reports are given in our own words.