Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
SQL

Many-to-Many Relationship in SQL

Resolve M:N with a junction table of two foreign keys.

Many-to-Many Relationship in SQL — a practical guide to SQL many-to-many relationship with clear examples you can reuse in real projects.

SQL Tutorial Series (81/100). Prefer one article? Read the complete SQL tutorial.

Short description

Add payload columns (e.g., enrolled_at) on the junction when needed.

M:N sketch

CREATE TABLE course_user (
  course_id INT UNSIGNED NOT NULL,
  user_id INT UNSIGNED NOT NULL,
  enrolled_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (course_id, user_id)
);

Leave a reply

Your email address will not be published. Required fields are marked *