Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > Change all foreign keys

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-22-07, 17:00
Prodoc Prodoc is offline
Registered User
 
Join Date: Mar 2004
Posts: 12
Change all foreign keys

Is it possible to change all foreign keys, in one go, which point to a specific primary key and make them point to a different primary key?

Example:
Person(person_id, name, dob, ...)
Class(class_id, teacher_id, name, start_date, ...)
Bus(bus_id, driver_id, name, mileage, ...)

Where teacher_id and driver_id contain a primary key person_id from Person.

Suppose one and the same person is a teacher for a class and a driver for a bus, a new person comes in and takes over both roles of the previous person.
What I would like to do is to change all foreign keys in every available table which point to the first person_id and make them point to the new person_id without having to specify this for each individual table. Is this possible?

I'm using MySQL 5.0 with InnoDB tables.
Reply With Quote
  #2 (permalink)  
Old 10-22-07, 17:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
make sure you declare your foreign keys with ON UPDATE CASCADE

that's exactly what this feature was designed for



p.s. ordinarily you would never update a person to change that person into a new person -- instead, it should be a deletion followed by an insertion
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 10-23-07, 09:35
Prodoc Prodoc is offline
Registered User
 
Join Date: Mar 2004
Posts: 12
I'm not sure I understand you correctly.

ON UPDATE CASCADE goes into effect when you change the primary key of the first person in the Person table. This, however, isn't the case. I'm adding a new person which gets a new primary key and the old person remains present. Knowing this, I don't quite see how ON UPDATE CASCADE would help in my situation. If I change the primary key of the first person, all foreign keys would change to the new primary key as well so the situation remains the same.
As far as I know I can't change the primary key of the new person to the primary key of the first person, followed by assigning a new primary key to the first person. This would cause you to have two identical primary keys in one table after the first step, which isn't allowed.

In addition, is it a desired effect to change the primary key of the first person in the first place?

Last edited by Prodoc : 10-23-07 at 09:40.
Reply With Quote
  #4 (permalink)  
Old 10-23-07, 09:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
you have an excellent grasp of the situation

the only way to get the cascade to work is by updating the existing primary key

whether you would really want to do that is questionable

sorry if i didn't make that too clear
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #5 (permalink)  
Old 10-24-07, 08:25
Prodoc Prodoc is offline
Registered User
 
Join Date: Mar 2004
Posts: 12
Correct me if I'm wrong but as far as I can tell this basically comes down to keeping track of all tables where a person has a foreign key and change the foreign key, with the newly created primary key, in each table one by one. Therefore the answer on my initial question would be "no, it's not possible" ;-)
Reply With Quote
  #6 (permalink)  
Old 10-24-07, 08:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
well, you could do it like this

1. change the first person's row (including the primary key) so that it now reflects the new person completely -- this would cascade the PK to all the related tables

2. add the first person back with her original PK
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #7 (permalink)  
Old 10-24-07, 09:13
Prodoc Prodoc is offline
Registered User
 
Join Date: Mar 2004
Posts: 12
Quote:
Originally Posted by r937
well, you could do it like this

1. change the first person's row (including the primary key) so that it now reflects the new person completely -- this would cascade the PK to all the related tables

2. add the first person back with her original PK
Indeed, you're right! I didn't think about that one, thanks.

Is it also possible to introduce some restrictions on the ON UPDATE CASCADE?
Suppose I want to change all foreign keys of the first person to a new person but only for a specific school_id.
To change the original table structure a bit:

Person(person_id, name, dob, ...)
Class(school_id, class_id, teacher_id, name, start_date, ...)
Bus(school_id, bus_id, driver_id, name, mileage, ...)

Now with an additional school_id in the primary key of Class and Bus.
Reply With Quote
  #8 (permalink)  
Old 10-24-07, 09:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
nope, sorry -- foreign key cascades operate on the foreign key regardless of what other columns are involved in the tables
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
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

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