Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many-to-many relationships examples

I haven't found any MYSQL many-to-many relationships examples here and in google. What I am looking is to see a very simple example with php+mysql showing database's results. Can anybody write a very simple example?

like image 984
good_evening Avatar asked May 27 '10 18:05

good_evening


People also ask

How do you do a many-to-many relationship?

A many-to-many relationship exists when one or more items in one table can have a relationship to one or more items in another table. For example: Your Order table contains orders placed by multiple customers (who are listed in the Customers table), and a customer may place more than one order.

What is an everyday example of a one to many relationship?

Here are some other examples of one-to-many relationships: People-Addresses (Each person can live at one address, but each address can house one or more people.) Owners-Pets (Each pet has one owner, but each owner can have one or more pets.)

What is one-to-many and many-to-many relationship?

One-to-many: A record in one table is related to many records in another table. Many-to-many: Multiple records in one table are related to multiple records in another table.

Why is many-to-many relationships a problem?

Many to Many(M:N) Relationship Many to many relationships create uncertainty and duplications on data that will eventually result in wrong statements for queries(2). In the below example; Each person can use many banks and each bank can have many customers.


2 Answers

Example scenario: students and courses at a university. A given student might be on several courses, and naturally a course will usually have many students.

Example tables, simple design:

CREATE TABLE `Student` (     `StudentID` INT UNSIGNED NOT NULL AUTO_INCREMENT,     `FirstName` VARCHAR(25),     `LastName` VARCHAR(25) NOT NULL,     PRIMARY KEY (`StudentID`) ) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci  CREATE TABLE `Course` (     `CourseID` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,     `Code` VARCHAR(10) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,     `Name` VARCHAR(100) NOT NULL,     PRIMARY KEY (`CourseID`) ) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci  CREATE TABLE `CourseMembership` (     `Student` INT UNSIGNED NOT NULL,     `Course` SMALLINT UNSIGNED NOT NULL,     PRIMARY KEY (`Student`, `Course`),     CONSTRAINT `Constr_CourseMembership_Student_fk`         FOREIGN KEY `Student_fk` (`Student`) REFERENCES `Student` (`StudentID`)         ON DELETE CASCADE ON UPDATE CASCADE,     CONSTRAINT `Constr_CourseMembership_Course_fk`         FOREIGN KEY `Course_fk` (`Course`) REFERENCES `Course` (`CourseID`)         ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=INNODB CHARACTER SET ascii COLLATE ascii_general_ci 

Find all students registered for a course:

SELECT     `Student`.* FROM     `Student`     JOIN `CourseMembership` ON `Student`.`StudentID` = `CourseMembership`.`Student` WHERE     `CourseMembership`.`Course` = 1234 

Find all courses taken by a given student:

SELECT     `Course`.* FROM     `Course`     JOIN `CourseMembership` ON `Course`.`CourseID` = `CourseMembership`.`Course` WHERE     `CourseMembership`.`Student` = 5678 
like image 91
Hammerite Avatar answered Sep 23 '22 04:09

Hammerite


Here's a quick and dirty example of the SQL involved. I don't see any need to muddy up the concept with php. Just retrieve the set like you would any other.

In this example, there are many names, and many colors. People are allowed to have more than one favorite color, and many people can have the same favorite color. Hence many to many.

 ***** Tables **********  person -------- id - int  name - varchar  favColor ------------- id - int  color - varchar  person_color ------------ person_id - int (matches an id from person) color_id - int (matches an id from favColor)    ****** Sample Query ******  SELECT name, color  FROM person      LEFT JOIN person_color ON (person.id=person_id)     LEFT JOIN favColor ON (favColor.id=color_id)   ****** Results From Sample Query *******  Name - Color --------------- John - Blue John - Red Mary - Yellow Timmy - Yellow Suzie - Green Suzie - Blue etc... 

Does that help?

like image 41
Syntax Error Avatar answered Sep 21 '22 04:09

Syntax Error