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 > Database Server Software > MySQL > Primary with Foreign Key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-22-11, 08:55
Eric the Red Eric the Red is offline
Registered User
 
Join Date: May 2008
Posts: 68
Primary with Foreign Key

Hi Guys, I'm really confused with how the foreign key works. I'm copying a database diagram into mysql for learning purpose and I came across a strange scenario:

Table Screens
================
screenid varchar(12) <PK, FK1> NOT NULL
hearderid varchar(12) <PK, FK2> NOT NULL
bodyid varchar(12) <PK, FK3> NOT NULL
comment varchar(40) NULL
================


So... question. Why is there a primary key with a foreign key and why is the PK and FK together. Also, why do we have multiple PK's.

I'm really confused any clarification would be awesome.
Reply With Quote
  #2 (permalink)  
Old 05-22-11, 09:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
let's take the second question first

there is only one PK, but it is a composite key, consisting of three columns

what that means is that the combinations of the three values are unique, even though a given value in any one of those three columns might occur multiple times

as for the FKs, each of them is a foreign key to its own separate table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-22-11, 10:15
Eric the Red Eric the Red is offline
Registered User
 
Join Date: May 2008
Posts: 68
Quote:
Originally Posted by r937 View Post
let's take the second question first

there is only one PK, but it is a composite key, consisting of three columns

what that means is that the combinations of the three values are unique, even though a given value in any one of those three columns might occur multiple times

as for the FKs, each of them is a foreign key to its own separate table

Thank you.

In this DB theres going to be about 10 normalized tables with similiar PK/FK relationships. Thing is that I have to MYSQLIMPORT the data first.

So, is it best to set the PK-FK after I import the data?

Do you have a sample schema of how such a PK-FK relationship would look in a create/alter table?

Does this look like a good translation?

Quote:
CREATE TABLE SCREENS
(
screenid varchar(12),
headerid varchar(12),
bodyid varchar(12),
comment varchar(40),
primary key (screenid, headerid, bodyid),
FOREIGN KEY (screenid) REFERENCES screens(id),
FOREIGN KEY (headerid) REFERENCES headers(id),
FOREIGN KEY (bodyid) REFERENCES bodyid(id)
)

Last edited by Eric the Red; 05-22-11 at 10:34.
Reply With Quote
  #4 (permalink)  
Old 05-22-11, 16:14
Eric the Red Eric the Red is offline
Registered User
 
Join Date: May 2008
Posts: 68
Does my create table look right (based on the table from the 1st post)?

Last edited by Eric the Red; 05-22-11 at 16:28.
Reply With Quote
  #5 (permalink)  
Old 05-22-11, 17:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Eric the Red View Post
Does my create table look right (based on the table from the 1st post)?
if those e id columns in the referenced tables are also VARCHAR(12), then yes

but isn't that a bit unusual? most people use numeric columns
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 05-22-11, 17:39
Eric the Red Eric the Red is offline
Registered User
 
Join Date: May 2008
Posts: 68
Quote:
Originally Posted by r937 View Post
if those e id columns in the referenced tables are also VARCHAR(12), then yes

but isn't that a bit unusual? most people use numeric columns
Thanks for the help! Yes it is strange that they are varchar(). I think they did this to protect the DB in case of future changes. Only thing that would make sense to me.
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