×

sql · level 2 · unit 7 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A pairs rows from two tables, one pair per row.

A student can join many clubs, and a club has many students. Neither table can hold the other's id, because one box holds only one value. So you build a link table in the middle.

Each row of memberships pairs one student id with one club id, and both are foreign keys. PRIMARY KEY (student_id, club_id) is a : each pair can appear only once.

To read it, join through the middle: memberships to students, and memberships to clubs.

SELECT s.name, c.club
FROM memberships m
JOIN students s ON m.student_id = s.id
JOIN clubs c ON m.club_id = c.id
ORDER BY s.name, c.club;

A composite key goes on its own line at the end of CREATE TABLE, after the columns.