Module 53: Relationships and ER Diagrams

Lesson 3 of 5 · 15–25 minutes

Many-to-many

🏠The everyday picture

Students and courses: a student takes several courses, and a course has many students. The school keeps an enrolment register: one line per student per course, with the date and fee paid.

📘What it really is

CREATE TABLE enrolments (
  student_id integer NOT NULL REFERENCES students(id),
  course_id integer NOT NULL REFERENCES courses(id),
  enrolled_on text NOT NULL,
  fee_paise integer NOT NULL,
  PRIMARY KEY (student_id, course_id)
) STRICT;

🖱️Try it

Create products and order_items (order_id, product_id, quantity, unit_price_paise) with a composite primary key. Add an order with three different products. Then change one product's price and check the order still shows the old price.

✅Quick check

Why can't a many-to-many relationship be stored with one foreign key?

Show the answer

Each side has many of the other; a single column can only point to one row.

Why does order_items store unit_price_paise when the product already has a price?

Show the answer

It records what was actually charged; product prices change later, but past orders mustn't.

← PreviousOne-to-many and one-to-one
ProLearners Assist