How to Design a Junction Table for Many-to-Many Relationships
title: "How to Design a Junction Table for Many-to-Many Relationships" summary: "A junction table stores each association between two entities as a row. This guide shows how to prevent duplicate links, store relationship-owned fields, account for repeated offerings, and choose foreign-key delete behavior in PostgreSQL 18." --- # How to Design a Junction Table for Many-to-Many Relationships
title: "How to Design a Junction Table for Many-to-Many Relationships" summary: "A junction table stores each association between two entities as a row. This guide shows how to prevent duplicate links, store relationship-owned fields, account for repeated offerings, and choose foreign-key delete behavior in PostgreSQL 18."
How to Design a Junction Table for Many-to-Many Relationships
A junction table represents a many-to-many relationship by storing the identifiers of both entities in one row per association. A student can take many courses and each course can have many students, so an enrollment table connects them. A composite primary key or a suitable unique constraint can prevent duplicate associations. PostgreSQL 18: Constraints
Why put the relationship in its own table?
Storing several course IDs in one student column makes filtering and updating awkward; storing a student list on each course has the same problem in the other direction. A junction table gives each association its own row and uses normalized foreign-key references. It can also hold fields that describe the relationship itself, such as enrollment time, grade, or status. Those values belong to a particular enrollment, not to the student or course alone.
Model student-course enrollment
CREATE TABLE student (
student_id bigint PRIMARY KEY,
student_name text NOT NULL
);
CREATE TABLE course (
course_id bigint PRIMARY KEY,
course_name text NOT NULL
);
CREATE TABLE enrollment (
student_id bigint NOT NULL REFERENCES student(student_id),
course_id bigint NOT NULL REFERENCES course(course_id),
enrolled_at timestamptz NOT NULL DEFAULT now(),
grade text,
PRIMARY KEY (student_id, course_id)
);This is illustrative PostgreSQL 18 DDL; it was not run. The composite primary key blocks a second row for the same student-course pair and makes both IDs non-null. The foreign keys require each link to point to an existing student and course. A primary key identifies rows and prevents duplicates; a foreign key checks referenced rows. PostgreSQL 18: Primary and Foreign Keys
Retrieve one student's courses through the association:
SELECT c.course_id, c.course_name, e.enrolled_at, e.grade
FROM enrollment AS e
JOIN course AS c USING (course_id)
WHERE e.student_id = $1
ORDER BY e.enrolled_at;When is a composite key not enough?
The pair (student_id, course_id) is appropriate only if a student may enroll in a course once. If the same course can be taken in multiple terms, model a course offering and key enrollment by (student_id, offering_id). If multiple attempts or status records can exist for one offering, use an enrollment identifier plus a UNIQUE constraint matching the real duplicate rule. A surrogate ID does not remove the need to state which repeated associations are allowed.
Choose deletion behavior and access paths
ON DELETE CASCADE can fit when an association cannot exist without its endpoints. If the association is historical or deletion must be blocked, use the default NO ACTION/RESTRICT behavior or retain a deactivated record. Choose from the lifecycle rules; do not cascade every foreign key automatically. PostgreSQL 18: Foreign Key Actions
Define what one association row means, decide whether duplicates are allowed, and choose a composite key or another key plus a matching unique constraint. Add foreign keys for both endpoints, place relationship attributes in the junction table, and choose deletion behavior. Consider common lookups: a composite index starting with student_id may support student-to-course searches, while searches by course_id alone may need a different index. Confirm with actual query patterns and plans; no benchmark was run.