Course Database Design

Relationships

The Fundamentals Preview

How entities connect

The three cardinalities

RelationshipExampleHow to model
One-to-oneUser ↔ ProfileForeign key on either side
One-to-manyCourse → LessonsForeign key on the "many" side
Many-to-manyStudent ↔ CourseA junction (join) table

One-to-many

COURSE 1 ── * LESSON\n\nCREATE TABLE lessons (\n  id INT AUTO_INCREMENT PRIMARY KEY,\n  course_id INT NOT NULL,      -- the FK\n  title VARCHAR(200),\n  FOREIGN KEY (course_id) REFERENCES courses(id)\n);

Many-to-many

STUDENT * ── * COURSE\n\nCREATE TABLE student_course (\n  student_id INT,\n  course_id INT,\n  enrolled_at DATETIME,\n  PRIMARY KEY (student_id, course_id),\n  FOREIGN KEY (student_id) REFERENCES students(id),\n  FOREIGN KEY (course_id)  REFERENCES courses(id)\n);

The junction table stores the connection — and any extra data about the connection (like the enroll date or grade).

Read the relationship aloud

"A student enrolls in many courses; a course has many students" — the word many on both sides tells you it needs a junction table.

Tip: Draw lines and write the numbers (1, *, 0..1) next to each entity first. Getting cardinality right on paper is half the design.

This is a free preview lesson

Enroll in the Database Design to unlock every lesson, lesson PDF guides, quizzes and your certificate - and start creating your own pieces today.


Discussion