Middle
How can a many-to-many relationship be established in a relational database?
sobes.tech AI
Answer from AI
A many-to-many relationship in a relational database is implemented through an intermediate (linking) table. This table contains foreign keys to the primary keys of the two related tables. For example, if there are tables Students and Courses, then a StudentCourses table is created for the many-to-many relationship with fields student_id and course_id.
Example structure:
CREATE TABLE Students (
id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE Courses (
id INT PRIMARY KEY,
title VARCHAR(100)
);
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)
);