Let's say I have a customers table with the following fields and records:
id first_name last_name email phone ------------------------------------------------------------------------ 1 Michael Turley mturley@whatever.com 555-123-4567 2 John Dohe jdoe@whatever.com 3 Jack Smith jsmith@whatever.com 555-555-5555 4 Johnathan Doe 123-456-7890
There are several other tables, such as orders , rewards , receipts , which have customer_id foreign keys related to this customers.id table.
As you can see, in their infinite wisdom, my users created duplicate entries for John Doe, complete with inconsistent spelling and missing data. The administrator notices this, selects clients 2 and 4 and clicks "Merge". Then they are asked to choose which value is suitable for each field, etc., And my PHP determines that the combined record should look like this:
id first_name last_name email phone ------------------------------------------------------------------------ ? John Doe jdoe@whatever.com 123-456-7890
Suppose that Mr. Dow placed several orders, received awards, generated receipts .. but some of them were associated with id 2, and some of them were associated with id 4. The combined row should correspond to all foreign keys in other tables, which match the source lines.
Here, where I'm not sure what to do. My instinct should do this:
DELETE FROM customers WHERE id = 4; UPDATE customers SET first_name = 'John', last_name = 'Doe', email = ' jdoe@whatever.com ', phone = '123-456-7890' WHERE id = 2; UPDATE orders, rewards, receipts SET customer_id = 2 WHERE customer_id = 4;
I think this will work, but if I add another table with the customer_id foreign key later, I must remember that I go back and add this table to the second UPDATE query in my merge function or risk losing integrity.
There must be a better way to do this.