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 > Help on a couple of tables for ERD

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-13-08, 01:10
Datanalysis Datanalysis is offline
Registered User
 
Join Date: Oct 2008
Posts: 23
Help on a couple of tables for ERD

Designing an ERD for a pet shop for school that does boarding and grooming. On the ERD we have the pet table with "pet_id" that has a many to many relationship with the color table that has colors and an attribute "color_id". Between them we have a composite table or "junction table" with both pet and color id as Foreign Keys and an artifical petcolor_id field as an Primary Key. Do we need this made up petcolor_id primary key field in there?
Reply With Quote
  #2 (permalink)  
Old 10-13-08, 03:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Datanalysis
Do we need this made up petcolor_id primary key field in there?
excellent question, and the answer is absolutely not

the PK of the junction table should be a composite PK consisting of the two FKs

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-13-08, 11:09
Datanalysis Datanalysis is offline
Registered User
 
Join Date: Oct 2008
Posts: 23
Thank you for your response. Yes currently that is how I have the junction table 1 primary key with 2 foreign keys, so this correct? Or do I just need 2 foreign keys that would cross reference each other for pet and color?
Reply With Quote
  #4 (permalink)  
Old 10-13-08, 14:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the junction table will have only 2 columns

(i prefer to call it a relationship table)

each of the 2 columns is a FK

together, the two columns are the PK
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-13-08, 15:11
Datanalysis Datanalysis is offline
Registered User
 
Join Date: Oct 2008
Posts: 23
what happens if I leave that PK in there? when it comes to programming etc, more work
Reply With Quote
  #6 (permalink)  
Old 10-13-08, 16:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
in that case you will need to define a separate UNIQUE constraint on the pair of FKs, to ensure that you don't link the same pet to the same colour more than once

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 10-14-08, 03:46
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
And you'll also have remember to mark both columns as non nullable

...or this will be valid input
Code:
+------+------+------+
| skey | col1 | col2 |
+------+------+------+
| 1    | ABC  | DEF  |
| 2    | NULL | DEF  |
| 3    | ABC  | NULL |
| 4    | NULL | XYZ  |
| 5    | NULL | NULL |
+------+------+------+
__________________
George
Twitter | Blog
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