sql server - Can I change foreign key IDs when deleting the FK row? -


I need to make one column unique in our database tables, and we completely delete any duplicates from the table. Want to However, this is a problem, in which there is a bunch of dependencies on other tables that will be affected. For example, suppose we have found the following relationships:

  ------------------------------ ----------- * Customer * * Order * ---- ---------------------------------- ----- * ID * * ID * * Name * * Customer ID * * Address * * Item * ------------------- ------- ------------  

There is a very clear relationship - an order needs a customer ID, and this is a foreign key, so we can not remove the customer. And keep order data. For example, I'm completely okay with the loss of redundant customer data, but in the latter context, I want to replace the customer ID with the order. Customer ID for "deleted customer"

Is there a way to say, "remove it, and if foreign key barriers, then change that ID to customer ID"? DB is MS SQL 2005.

I will say:

Step 1: To keep a list of duplicate IDs Removal method with respect to the ID depends on the fact that you are detecting the duplicate. Say you get a table:

  ------------- * Dupes * ------------- * del_id * * Keep_id * -------------  

Step 2: relink command

  update order o customer ID = (Choose keep_id from) Duplex D where d.del_id = o.CustomerID) in customer ID (choose from Dupus del_id)  

Step 3: Delete old customers

 Remove from  Customer where in the id (choose from Dupus to del_id)  

and voice.


Comments