Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In real-world business models, entities frequently have mutual multiple relationships:
Relational database engines cannot directly connect two tables in a Many-to-Many (M:N) relationship using a single foreign key. Instead, you resolve an M:N relationship by decomposing it into two 1:N relationships using an intermediate Junction Table (also called a Bridge Table, Pivot Table, or Associative Entity).
A junction table contains at minimum two Foreign Keys, each pointing to the primary key of one of the participating parent tables:
A junction table does not just link IDs; it can store attributes of the relationship itself (known in ER modeling as an associative entity):
order_items (linking orders and products): stores quantity and unit_price_at_purchase.student_courses: stores enrolled_at, completion_status, and final_grade.movie_cast: stores character_name and billing_order.Querying an M:N relationship requires performing two consecutive JOINs through the junction table:
PRIMARY KEY (student_id, course_id) automatically prevents duplicate enrollments. If the junction table itself needs to be referenced by child tables, you may add a surrogate primary key (id INT AUTO_INCREMENT) and apply a UNIQUE KEY (student_id, course_id).(student_id, course_id) automatically indexes queries filtering by student_id. However, queries filtering by course_id cannot use the leftmost prefix! You must add an explicit index on course_id in the junction table for reverse lookups.quantity or discount in an order_items table) called?(author_id, book_id), why should an explicit index on book_id be created?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| One-to-Many (1:N) Relationships | Entity-Relationship (ER) Modeling & Schema Diagrams |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.