Composite primary keys
A composite primary key: two or more fields together identify each record uniquely. Each field can repeat alone, but the pair cannot. Used in junction tables.
Understanding Composite primary keys in WA Computer Science ATAR Unit 2
What it does: studentId 1 repeats and subjectId 1 repeats, but no (studentId, subjectId) pair repeats, so the pair can be the key. The junction table's key is made of the two foreign keys, and each one still references its own parent table. A table has only one primary key. A composite key is a single key made up of several fields combined. The combination must be unique. (1, 2) twice is a duplicate pair. (1, 2) and (1, 3) differ in subjectId, so they are allowed. They link to students and subjects, so they are foreign keys. Together they also make up the composite primary key.
Common mistakes: studentId alone is not unique, because a student enrols in many subjects. Declaring it as a PRIMARY KEY by itself is wrong. Remove it here and keep the pair in the single composite PRIMARY KEY line: ' studentId INT,'. The two fields are missing a comma between them. The correct line is 'SELECT studentId, subjectId'. Together these two fields identify one enrolment row.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 28–122) and word search (Modules 28–266).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
PRIMARY KEY (studentId, subjectId)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →