I need to make a column unique in one of our database tables, and we want to completely remove any duplicates from the table. There is however a snag, in that there are a bunch of dependencies to other tables that will be affected.
For example, let’s say we’ve got the following relationship:
------------------- -------------------
* Customer * * Order *
------------------- -------------------
* ID * * ID *
* Name * * CustomerID *
* Address * * Item *
------------------- -------------------
Fairly obvious relationship there – an Order needs a CustomerID, and that’s a foreign key. So we can’t delete the customer and keep the Order data.
In the example I’m totally ok with losing the redundant customer data, but for later reference I’d like to change the CustomerID from Order to a Customer ID for a “Deleted Customer”-row.
Is there any way of saying “delete this, and if there are foreign key constraints, change the CustomerID to that ID instead”? The DB is MS SQL 2005.