Updating Foreign Keys with a new ID -
11-27-2007
, 09:42 AM
I have a table called 'tags' with id (integer auto-increment) and
title varchar(200).
Other tables reference these tags through a foreign key that refrences
the tag table's id column. (Example: table 'articles' with a column of
'tag_id' integer)
There comes a time when a user might want to migrate all items
associated with one tag to another. (For example: User realizes
they've inserted a tag called 'Sports' and at one time also used one
called 'Soprts') The user needs to migrate all items tagged with
'Soprts' to 'Sports.' Let's say there are 10 other tables in the
database that reference the id in the tags table via a foreign key
I want to avoid manually updating all of these ids (example 'UPDATE
articles SET tag_id = @new_id WHERE tag_id = @old_id) because I don't
want to have to rely on manually ensuring each table gets handled
correctly.
Is there an easy way to dynamically update all of these tables with
the new id so that the records are associated to the correct tag,
without calling them all individually? |