dbTalk Databases Forums  

Updating Foreign Keys with a new ID

mailing.database.sql-general mailing.database.sql-general


Discuss Updating Foreign Keys with a new ID in the mailing.database.sql-general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Matt Jimison
 
Posts: n/a

Default 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?

Reply With Quote
Reply




Thread Tools
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 On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.