Skip to content
SPM Tuition
Computer Science · Relational data modelling

Resolving a many-to-many relationship

Students join many clubs and clubs have many students, and one foreign key cannot hold it.

A many-to-many relationship needs a third table. The intermediate table holds one row per link and carries the primary keys of both sides.

This lesson belongs to relational data modelling. It builds on reading an entity relationship diagram.

Why does one foreign key fail?

Suppose each Student row carries a ClubID. That works only if a student joins one club. Aina joins Chess and Robotics, so her single ClubID cell would need two values.

Swap the direction and the same problem appears: a club has many students, so one StudentID in Club cannot hold them all.

Worked example: students and clubs

Start with the two entities.

Student: S01 Aina, S02 Bala, S03 Chen.

Club: C1 Chess, C2 Robotics.

The facts are that Aina joins C1 and C2, Bala joins C2, and Chen joins C1. Put each link in its own row of a new table, Membership.

StudentID ClubID JoinTerm
S01 C1 Term 1
S01 C2 Term 2
S02 C2 Term 1
S03 C1 Term 1

The primary key is the pair (StudentID, ClubID). StudentID repeats (S01 twice) and ClubID repeats (C1 twice), but no pair repeats. Both columns are also foreign keys, pointing to Student and Club.

The result is two one-to-many links: one student has many memberships, and one club has many memberships.

The mistake that costs marks

Students write a list in one cell, such as ClubID = “C1, C2”. It looks compact, but it breaks the one-value-per-cell rule.

Design Can you count Chess members? Can you add a join term?
list in one cell no, the cell must be split first no
intermediate table yes, count rows with C1 yes, add a JoinTerm column

The fix is to ask whether both sides say “many”. If they do, create a table that links them.

Check yourself

A school stores Teachers and Subjects. A teacher can teach many subjects, and a subject is taught by many teachers. Name the intermediate table, its columns and its primary key.

Answer

A table such as Teaching, with columns TeacherID and SubjectID. The primary key is the pair (TeacherID, SubjectID). Each column is also a foreign key, pointing to Teacher and Subject.

What to study next

An intermediate table avoids one kind of repeated data. Continue with explaining update anomalies before normalising, then try the relational data modelling practice set.

If you want a teacher to go through your design, see online one-to-one Computer Science tuition.

Common questions

What is an intermediate table?

It is a table whose job is to link two other tables. It holds the primary key of each and sometimes extra facts about the link, such as the term the student joined. It turns one many-to-many relationship into two one-to-many relationships.

What is the primary key of an intermediate table?

Usually the two foreign keys together, called a composite key. The pair StudentID and ClubID identifies one membership. Neither column alone is unique, but the pair is.

Why can I not use a list in one cell?

A cell holding 'C1, C2' breaks the rule that each cell holds one value. You cannot search, count or link that cell cleanly. The intermediate table stores one link per row instead.

If many-to-many questions still confuse you, one-to-one Computer Science lessons let a teacher hand you a scenario and watch you build the intermediate table.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.