Sobes.tech
Junior — Middle

How is a many-to-many relationship implemented in relational database management systems?

sobes.tech AI

Answer from AI

In relational DBMS, a many-to-many relationship is implemented by creating an additional join table or junction table. This table contains foreign keys referencing the primary keys of the two related tables.

For example, there are tables Students and Courses. One student can attend many courses, and one course can have many students. To establish this relationship, a StudentCourses table is created with fields student_id and course_id:

CREATE TABLE StudentCourses (
  student_id INT,
  course_id INT,
  PRIMARY KEY (student_id, course_id),
  FOREIGN KEY (student_id) REFERENCES Students(id),
  FOREIGN KEY (course_id) REFERENCES Courses(id)
);

Thus, a many-to-many relationship is realized through an intermediate table that stores pairs of related records.

How is a many-to-many relationship implemented in… - sobes.tech