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 do I resolve this situation with many to many

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-29-08, 00:00
Datanalysis Datanalysis is offline
Registered User
 
Join Date: Oct 2008
Posts: 23
How do I resolve this situation with many to many

Hello, database is on a pet shop, heres my format on three tables that are related on the ERD our rule is assigning 3 main colors to 1 pet for identification. It is a many-to-many relationship between pet and colors.

Pet table
---------
pet_id PK



Composite table
-----------
pet_id FK
color_id FK


Color table
-----------
color_id PK
color desc


Is this right? or should I have in the composite table pet_id FK and 3 FK color_id fields that would have each a color referencing the color table? Appreciate your help.
Reply With Quote
  #2 (permalink)  
Old 10-29-08, 04:41
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
What you've described so far appears AOK to me
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-31-08, 13:35
Datanalysis Datanalysis is offline
Registered User
 
Join Date: Oct 2008
Posts: 23
How would I go about coding the composite table?
Reply With Quote
  #4 (permalink)  
Old 11-03-08, 17:49
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
Something along the lines of ...
Code:
Create Table PetColor
  (ColumnA  ColumnType Not Null,
   ColumnB  ColumnType Not Null)
add foreign key relationships to ColumnA and ColumnB to reference your two MANY tables, and your compound primary key.

ref http://www.1keydata.com/sql/sqlcreate.html
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #5 (permalink)  
Old 11-03-08, 18:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
CREATE TABLE petcolours
( pet_id      INTEGER NOT NULL 
, colour_id   INTEGER NOT NULL 
, CONSTRAINT pet_fk
    FOREIGN KEY ( pet_id ) REFERENCES pets ( pet_id )
, CONSTRAINT colour_fk
    FOREIGN KEY ( colour_id ) REFERENCES colours ( colour_id )
, PRIMARY KEY ( pet_id, colour_id )
);
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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