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 > Auto-increment on FK

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-24-11, 17:54
JHamill JHamill is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Auto-increment on FK

Finally figured how to implement foreign key relationships in MySQL, my question is should I set the foreign key fields in the child table to auto-increment, or is this already dealt with because they are A_I is set in the parent table?

For instance:
UserTbl
---------
userID [pk]
firstname
lastname
departmentID [fk]

---------
DepartmentTbl
----------
departmentID [pk]
departmentTitle

From the example above, the departmentID in the UserTbl be set to auto-increment or is this dealt with through the FK relationship?
Reply With Quote
  #2 (permalink)  
Old 02-24-11, 19:21
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by JHamill View Post
... should I set the foreign key fields in the child table to auto-increment, or is this already dealt with because they are A_I is set in the parent table?
neither

first of all, you shouldn't make it an auto_increment, it'll get out of step immediately

but it isn't "taken care of" by the parent table, either

you actually have to specify the parent primary key value as a value to be inserted when you insert a child row
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-25-11, 05:36
JHamill JHamill is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Thank you for your quick response again r937.

Is this done by auto cascade on update?
Reply With Quote
  #4 (permalink)  
Old 02-25-11, 05:46
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by JHamill View Post
Is this done by auto cascade on update?
no, you have to do it with application code
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-25-11, 05:50
JHamill JHamill is offline
Registered User
 
Join Date: Jun 2009
Posts: 5
Oh I see now - should have noticed that beforehand really.

Thanks again.
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