Jeffrey
Jeffrey

Reputation: 1667

Changing multiple foreign keys to point to a new table

On MS SQL Server 2005, I have a table that I want to replace. Many other tables have a Foreign Key reference to this table. Is there a script that I can run to simply roll over all these references from the old table to the new table?

Or am I going to have to go through and specifically drop each of these foreign keys, and write a new key for each one?

Upvotes: 0

Views: 280

Answers (2)

Charles Bretana
Charles Bretana

Reputation: 146449

Or, can you re-use the same Primary Keys for the new table that were in the Old table you are replacing? Then you don't have to update the FKs at all.

If the Primary Key is auto-incrementing "Identity" column, you can use "Set Identity_Insert On", to allow you to insert new records into the new table's identity column with an explicit value (the pk from the old table). Run Set Identity_Insert Off when you're done and you're off and running.

Upvotes: 1

Otávio Décio
Otávio Décio

Reputation: 74250

You are safer dropping and recreating them for the new table.

Upvotes: 1

Related Questions