I'm trying to de-duplicate user accounts in our system and I know there are lots of questions out there about removing/identifying duplicates (such as Remove duplicate rows in MySQL), but I haven't seen any that required maintaining referential records.
I have a users
table and a subscriptions
table with a foreign key field User_ID
common to both and set to CASCADE
in subscriptions
.
I'd like to remove all duplicates in the users
table but in doing so, all of the records corresponding to User_ID
in the subscriptions
table would be lost due to the CASCADE
behavior.
Is it possible to UPDATE
the users
table, altering the User_ID
of the duplicate records to the one I want to keep, without colliding with the unique index, allowing all referential records to be updated accordingly and finally removing the duplicate User record without cascading the delete?
The added complication is that the User_ID
field in the users table is obviously indexed with unique
.
EDIT: I should add that this is a simplified example, our DB has 100+ tables many of which have foreign keys based on the User_ID.