Junction Table
ELI5 — The Vibe Check
A junction table (also called a join table) is the middle table you create to represent a many-to-many relationship. It has two foreign keys — one pointing to each of the two related tables. The students-courses problem is solved by an enrollments table.
Real Talk
A junction table (associative table, bridge table, or join table) resolves many-to-many relationships by creating a table with foreign keys to both related tables. Each row represents one association between the two entities. It can also store attributes of the relationship itself (e.g., enrollment date).
Show Me The Code
-- post_tags is a junction table
CREATE TABLE post_tags (
post_id INT REFERENCES posts(id) ON DELETE CASCADE,
tag_id INT REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (post_id, tag_id)
);
When You'll Hear This
"Add a junction table to handle the many-to-many relationship between posts and tags." / "The junction table can also store metadata about the relationship."
Related Terms
ERD (Entity Relationship Diagram)
An ERD is a visual map of your database — boxes for tables, lines showing how they connect.
Foreign Key
A foreign key is how you link two tables together. If an 'orders' table has a 'user_id' column pointing to the 'users' table, that is a foreign key.
Many-to-Many
Many-to-Many means rows on both sides can relate to many rows on the other side. Students can enroll in many courses, and courses can have many students.
Pivot Table
Pivot Table means two different things. In Laravel/PHP it is just another name for a junction table.