Skip to content

Junction Table

Medium — good to knowDatabase

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.