473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Help with MySQL Many to Many Relationship

2
Hello,

I'm new to database development and need your help.

Although, I'm new I know that storing multiple entries into a database field is a horrible idea. Therefore, I'm trying to implement cross-reference/look-up tables.

Here's the problem I'm trying to solve...

I'm working on a restaurant guide site with restaurants having multiple dishes and dishes possibly being assigned to multiple restaurants.

Here is the structure of the two database tables:

restaurant
id
categoryid
name
price

dish
id
name

I setup the tables using the following codes:

CREATE TABLE restaurant (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
categoryid INT,
name VARCHAR(255) NOT NULL,
price VARCHAR(5) NOT NULL,
FOREIGN KEY (categoryid) REFERENCES category(id) ON DELETE SET NULL
) TYPE=INNODB;

CREATE TABLE category (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL
) TYPE=INNODB;

CREATE TABLE dish (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(25) NOT NULL
) TYPE=INNODB;

Here's how I setup the xref table:

CREATE TABLE restdish (
restaurantid INT NOT NULL,
dishid INT NOT NULL,
PRIMARY KEY(restaurantid, dishid)
) TYPE=INNODB;

With this all said and done I have a few questions:
  1. Will I need to go into the xref table and add in all the possible restaurant to dish id combinations?
  2. Am I missing a relationship between the restaurant and dish tables?

I'm thinking I need to place a restaurantid foreign key in the dish table to assign each dish combination to restaurants but then dishes will need to be repeated over and over again. I'm just thinking there's a better way to set things up.

Your help will be sorely appreciated!

OD Ntuk
Jan 22 '07 #1
5 3855
ronverdonk
4,258 Expert 4TB
Please read the Posting Guidelines before you post in this forum!.

This is a MySQL related question, and has nothing to do with PHP, so the PHP thread has been removed.

moderator
Jan 22 '07 #2
odntuk
2
Please read the Posting Guidelines before you post in this forum!.

This is a MySQL related question, and has nothing to do with PHP, so the PHP thread has been removed.

moderator
I apologize. I posted in the PHP section before I noticed that there was a MySQL section.
Jan 22 '07 #3
gyung
21
I suggest physically drawing out tables and draw lines to represent relationships. I usually do this when making any more than 3~4 tables just to make sure I'm not missing anything. When you have 10+ tables it really helps :D
Jan 22 '07 #4
ronverdonk
4,258 Expert 4TB
I suggest physically drawing out tables and draw lines to represent relationships. I usually do this when making any more than 3~4 tables just to make sure I'm not missing anything. When you have 10+ tables it really helps :D
You are absolutely right. In my opinion, drawing a database diagram must always be part of the normal design procedure. Without it you, or anyone after you who does maintenance on the code, are stuck when, say after 6 months, you have to re-design one or more tables and you don't have a clue as to how you did it in the past.

Ronald :cool:
Jan 26 '07 #5
cassbiz
202 100+
You are absolutely right. In my opinion, drawing a database diagram must always be part of the normal design procedure. Without it you, or anyone after you who does maintenance on the code, are stuck when, say after 6 months, you have to re-design one or more tables and you don't have a clue as to how you did it in the past.

Ronald :cool:
I recently had to redraw a complete table structure because I "winged" the first one and forgot some necessary fields.

Piece of paper or white board works real well.

Good Luck
Jan 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the...
0
by: | last post by:
Hi, I'm sure I'm missing something simple. I've searched the doc but can't = find an answer. I downloaded and installed MySQL for Windows this morning, V4.1. = Unzipped, installed and...
0
by: Tarbuza Smith | last post by:
I was told that it is easy to create a table in MYSQL but it is hard to create a table with relationship like in Oracle or SQL Server by using Relationship or Query by Design or whatever wysiwyg...
0
by: Vaclav Frolik | last post by:
CHARONWARE, s.r.o. is pleased to announce that a new version of CASE Studio 2 version 2.18 has been released. CASE Studio 2 allows database designers and developers to visualy create and...
4
by: Simon Bond | last post by:
Hi there, I should point out I am very new to MySql, im trying to learn what I can. Basically im using Navicat to work with my databases. When making a foreign key, i am asked to enter the...
2
by: Martin | last post by:
Is there any way in MySQL Admin to generate a print-out that shows the structure of a table in a database? I really don't like having to document this info by hand writing it on pieces of paper. ...
20
by: Jack Schitt | last post by:
I thought I was starting to get a handle on Access, until I tried doing something useful...now I'm stuck. I have a DB with two tables - to keep it simple I'll say that one is an Employee File...
6
by: Ridge Burner | last post by:
Can someone tell me which of these 2 SQL queries will be more efficient? I'm having a debate with another guy about which would be less resource intensive for MySQL. The first uses MySQL to pick...
118
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.