Avatar of FairyBusiness
FairyBusiness
Flag for United States of America asked on

How can I create a many to many relationship in mysql?

Hi, my professor this last semester showed us how to do a many to many relationship in MySQL, but he only used 1 day to go over it and I did not fully understand it at the time. So now I need to have a many to many relationship, but I am not sure how to create this. (trying to avoid listed fields) Not even sure where to start.  (I have only ever done one to many relationships)

If someone could please describe a SIMPLE way of doing this, or knows of a SIMPLE tutorial that would be great!
MySQL Server

Avatar of undefined
Last Comment
FairyBusiness

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kent Olsen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
alexjfisher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen



A many-to-many relationship does not require a third table.


Kent
FairyBusiness

ASKER
I dont get how you would do a many to many relationship without a third table??  which table are the rows that relate to multiple rows on both sides of the two tables???
SOLUTION
Kent Olsen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

Taking this a step further, you could actually put all of the data in the books table.  The many-to-many relationship occurs because you can have the same author occur several times and you can have the same title occur several times.


Kent
Your help has saved me hundreds of hours of internet surfing.
fblack61
FairyBusiness

ASKER
but if you have more than one item on the other side needing the title how do you specify where the title goes to which at what time?  i'm just not getting it. .. I know how to create tables and give things the same name but that doesnt help me create a many to many relationship bc i'm not looking at it any new way than before
Kent Olsen

Ok.  Back to the books table.

INSERT INTO books (isbn, author, title) VALUES ('00001', "Alan", "English Dictionary");
INSERT INTO books (isbn, author, title) VALUES ('00002', "Barb", "Latin for Beginners");
INSERT INTO books (isbn, author, title) VALUES ('00003', "Chad", "English Dictionary");
INSERT INTO books (isbn, author, title) VALUES ('00004', "Dave", "What to Wear");
INSERT INTO books (isbn, author, title) VALUES ('00005', "Earl", "Beginning Math");
INSERT INTO books (isbn, author, title) VALUES ('00006', "Dave", "Peyton Place");

A many-to-many relationship exists between the author and title columns.  Dave has written two books ("What to wear" and "Peyton Place").  A book titled "English Dictionary" was written by both Alan and Chad.

If the Authors and Title are moved into separate tables the many-to-many relationship exists across tables.


Kent
FairyBusiness

ASKER
It still looks like a one to many relationship in my mind. . .  

you have one book english dictionary, two authors

or you have one author dave, and two books


maybe I am missing something. . .
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
alexjfisher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Kent Olsen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FairyBusiness

ASKER
ok only the 3 tables makes any sense to me. .. so can someone give me an example of how that would look?
FairyBusiness

ASKER
nope that didnt work.

here is my entire function if it helps:

function woods() {
	global $conn;
	if(isset($_GET['id'])) {
		$id = $_GET['id'];
		$query = "SELECT name FROM submenus WHERE id=" . $id;
		$result = mysql_query($query, $conn);
		confirm_query($result);
		while($rows = mysql_fetch_assoc($result)) {
			$name = $rows['name'];
		}
		$path = "flipbook/" . $name;
		$dirs = scandir($path);
		foreach($dirs as $dir) {
			echo $dir . "<br />";
			$data = $path . "/" . $dir;
			echo $data . "<br />";
			$style = scandir($data);
			//print_r($style[4]);
			if(!is_dir($style)) {
				foreach($style as $img) {
					if (!is_dir($img)){
						echo "<img src=\"" . $data . "/" . $img . "\" />";
					}
				}
			}			
		echo "<br />";
		}
	}
}

Open in new window

FairyBusiness

ASKER
ignore that last comment, was for another question.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Kent Olsen

Hi Fairy,

Don't think of the database structure, think of the data.  The relationship is between data objects.  The container is immaterial.


Kent
FairyBusiness

ASKER
ok, I have 3 necklaces

they each contain many gemstones:

rubies
diamonds
sapphires
quartz

so all 3 necklaces contain rubies, so rubies would to three different ones with 3 coming at it

how would that work specifically? so me a row that demonstrates the many to many relationship
Kent Olsen


Easy enough.

Necklace 1   Rubies
Necklace 1   Diamonds
Necklace 1   Sapphires
Necklace 1   Quartz
Necklace 2   Rubies
Necklace 3   Rubies
Necklace 3   Diamonds

Open in new window


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FairyBusiness

ASKER
what if I have like 50 gemstones per necklaces? and like 50 necklaces?
Kent Olsen

The volume doesn't matter, as long as each side of the relationship supports a quantity greater than 1.

SOLUTION
dportas

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
alexjfisher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FairyBusiness

ASKER
thanks everyone
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy