If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > how to create many-to-many relation?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-11-09, 08:34
emilh emilh is offline
Registered User
 
Join Date: Mar 2009
Posts: 27
Question how to create many-to-many relation?

I'm at the stage of designing a database for a small restaurant programm and cannot solve one problem. on the picture you can see the problem. i have 2 tables:

Products
Menu/Dishes

Dishes can consist of severeal products and products in turn can be a part of several dishes. how to implement this in my case? Should I create any intermediate table for it or something?

Thank you in advance!
Attached Thumbnails
how to create many-to-many relation?-many-many.jpg  
Reply With Quote
  #2 (permalink)  
Old 03-11-09, 12:26
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Yes, you create an intermediate table. It's primary key will include the primary keys from your other two tables.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 03-13-09, 04:02
emilh emilh is offline
Registered User
 
Join Date: Mar 2009
Posts: 27
Blindman, I got it thank you for help!

Last edited by emilh; 03-13-09 at 04:49.
Reply With Quote
  #4 (permalink)  
Old 03-13-09, 04:31
emilh emilh is offline
Registered User
 
Join Date: Mar 2009
Posts: 27
Blindman, I got it thank you for help!

Last edited by emilh; 03-13-09 at 04:49.
Reply With Quote
  #5 (permalink)  
Old 03-22-09, 03:00
shwe shwe is offline
Registered User
 
Join Date: Feb 2009
Posts: 54
I would like to ask more in-depth this question:
in the intermediate table is it that the referencing rows MUST form a primary key, or can I create an "indipendent" primary key and just reference the rows without constraints to other tables? Actually I've already tried it and it seems to work, but I don't know if I'm missing anything. I need this to be able to leave one column "null" of three or four referenced tables. With my previous design postgres would retreive unwanted rows.
I hope I'm being clear enough.
Reply With Quote
  #6 (permalink)  
Old 03-23-09, 10:37
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
No, there is no requirement that the foreign keys must form a composite primary key.
But you should enforce uniqueness on the pair, and a primary key is a simple method of achieving that. The other option is to create a unique non-clustered index or a constraint.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #7 (permalink)  
Old 03-23-09, 11:49
shwe shwe is offline
Registered User
 
Join Date: Feb 2009
Posts: 54
and what undesirable effects would have not enforcing uniqueness on the pair?
Reply With Quote
  #8 (permalink)  
Old 03-23-09, 12:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by shwe
and what undesirable effects would have not enforcing uniqueness on the pair?
search: removing duplicates 2,960,000 results

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 03-23-09, 20:52
shwe shwe is offline
Registered User
 
Join Date: Feb 2009
Posts: 54
haha, smart answer!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On