01 / 19

Bridge Tables
in SQL

Master the essential technique for modeling many-to-many relationships in relational databases using bridge (junction) tables.

SQL

What is a Relationship in a Database?

In relational databases, a relationship describes how rows in one table connect to rows in another table. These connections are established using primary keys and foreign keys.

One-to-One
Each row in Table A matches exactly one row in Table B.
One-to-Many
One row in Table A can match many rows in Table B.
Many-to-Many
Many rows in A can match many rows in B: the trickiest type.

The Many-to-Many Problem

Consider Students and Courses: one student can enroll in many courses, and one course can have many students. You cannot represent this with a single foreign key in either table.

Students Alice Bob Carol Dave Courses Math Science English ✕ Can't use a single FK!

Enter the Bridge Table

A bridge table (also called a junction table, join table, or associative table) is a third table that sits between the two original tables and converts a many-to-many relationship into two one-to-many relationships.

Students student_id (PK) name Enrollments student_id (FK) course_id (FK) enrolled_date Courses course_id (PK) title N:1 N:1 Bridge table converts M:N into two 1:N

The Core Rule of Bridge Tables

1
Foreign Key A: References the primary key of the first entity table.
2
Foreign Key B: References the primary key of the second entity table.
3
Composite PK: The combination of (FK_A, FK_B) uniquely identifies each pairing.
4
Optional attributes: Extra columns (e.g., date, role, quantity) describe the relationship itself.

Anatomy of a Bridge Table

student_courses (Bridge Table) student_id course_id grade 1 101 A 1 102 B+ 2 101 A- 3 103 B PK = (student_id, course_id) FK FK

Bridge Table vs. Regular Table

Bridge Table Regular Entity Table
Primary Purpose Link two entities together Store data about one entity
Primary Key Composite (two FKs) Single column (often auto-increment)
Foreign Keys Always has at least 2 May have 0 or more
Own Data Optional (attributes of the link) Core entity attributes
Naming Combines both entity names Singular noun (e.g., Student)

How to Create a Bridge Table

1
Identify the M:N relationship. Ask: "Can one A relate to many B, AND one B relate to many A?"
2
Create the bridge table. Name it after both entities (e.g., student_courses) or the relationship verb (e.g., enrollments).
3
Add two foreign key columns referencing the PKs of each entity table.
4
Set the composite primary key as the combination of both FK columns.
5
Add optional attributes (date, status, quantity) that describe the relationship itself.

Example 1: Creating the Tables

CREATE TABLE students ( student_id INT PRIMARY KEY, name VARCHAR(100) ); CREATE TABLE courses ( course_id INT PRIMARY KEY, title VARCHAR(100) ); -- Bridge table CREATE TABLE enrollments ( student_id INT, course_id INT, grade CHAR(2), PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES students(student_id), FOREIGN KEY (course_id) REFERENCES courses(course_id) );

Example 2: Querying Through a Bridge

Goal: Find all courses that student "Alice" is enrolled in.

SELECT c.title FROM students s JOIN enrollments e ON s.student_id = e.student_id JOIN courses c ON e.course_id = c.course_id WHERE s.name = 'Alice';

Example 3: Authors and Books

A book can have multiple authors, and an author can write multiple books. This is a classic many-to-many scenario.

authors author_id (PK) name country book_authors author_id (FK) book_id (FK) PK = (author_id, book_id) books book_id (PK) title isbn N:1 N:1
-- Find all authors of "Database Design" SELECT a.name FROM authors a JOIN book_authors ba ON a.author_id = ba.author_id JOIN books b ON ba.book_id = b.book_id WHERE b.title = 'Database Design';

Real-World Bridge Tables Everywhere

Actors + Movies
Bridge: cast_members with actor_id, movie_id, and role name.
Products + Orders
Bridge: order_items with order_id, product_id, and quantity.
Users + Roles
Bridge: user_roles with user_id and role_id.

Optional Attributes on the Bridge

Bridge tables often carry extra columns that describe the relationship itself, not the entities. These attributes only make sense in the context of both entities together.

Bridge Table FK Columns Relationship Attributes
enrollments student_id, course_id grade, enrollment_date
order_items order_id, product_id quantity, unit_price
cast_members actor_id, movie_id role_name, billing_order
playlist_songs playlist_id, song_id position, added_date

Composite PK vs. Surrogate Key

Some developers add a separate auto-increment ID to the bridge table instead of using a composite primary key. Both approaches work.

Composite PK
PRIMARY KEY (student_id, course_id): Automatically prevents duplicate pairings. No extra column needed.
Surrogate PK
id INT PRIMARY KEY + a UNIQUE constraint on (student_id, course_id). Simpler to reference from other tables.

Common Mistakes to Avoid

✕ Comma-Separated IDs
Storing multiple IDs in a single column like "101,102,103" violates first normal form and makes queries nearly impossible.
✕ No Primary Key
Forgetting to set a primary key on the bridge table allows duplicate pairings (e.g., same student enrolled in same course twice).
✕ Missing Foreign Keys
Not defining FK constraints means orphaned records can appear: references to students or courses that no longer exist.

Question 1 of 5

What is the primary purpose of a bridge table in a relational database?

Question 2 of 5

Which of the following is the minimum set of columns a bridge table must contain?

Question 3 of 5

Given tables actors and movies, a bridge table cast_members has columns (actor_id, movie_id, role_name). What serves as the composite primary key?

Question 4 of 5

A developer stores course IDs as a comma-separated string in the students table: "101,102,103". Why is this a problem?

Question 5 of 5

To find all students enrolled in "Biology", how many JOIN operations are needed when using a bridge table?