on 2014 Jan 05 3:07 PM
We use Declarative RI throughout our application and it has the benefit of tying together customer accounts and their related transactions. However there are cases where we find that there have been inadvertent duplicate account entires and we want to merge all the references from the duplicates to a single account reference and eliminate the duplicate accounts.
So while we can change all references to an account by cascade update from the account, we cant merge references because of the duplication.
Are their any elegant ways to utilize existing RI to remap references, or is it pretty much brute force SQL updates on all the referencing tables?
Is this what you are trying to do?
UPDATE child_table SET child_table.foreign_key = 'correct parent_table.primary_key value' WHERE child_table.foreign_key = 'incorrect parent_table.primary_key value';
I don't know if that qualifies as "brute force" since a single UPDATE takes care of all transactions for a single account. However, if you have hundreds or thousands of duplicated accounts to deal with, then perhaps a fancier solution would be worth constructing.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I guess it's more than that: If my understanding is correct, then the child_table might have duplicate values itself, i.e. the above UPDATE statement would lead to the situation where afterwards there are duplicate references to the same (merged) account.
Therefore one would need some statement with
a) a branch that updates references to the duplicate account not yet existing for the correct account, so that they point to the correct account) and b) a branch that deletes references to the duplicate account that already exist for the correct account.
That sounds pretty much like a perfect case for the MERGE statement with branches WHEN MATCHED AND <duplicate does exist> THEN DELETE WHEN MATCHED AND <duplicate does not exist> THEN UPDATE <the reference to the correct account>...
However, not knowing what makes a "reference a duplicate", I feel unable to show a small sample.
User | Count |
---|---|
66 | |
10 | |
10 | |
10 | |
10 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.