In a MySQL database, I have a table full of business information. Lots of businesses have been inserted twice due to human error, so the same information exists with two different primary keys. Let's say the same delivery service is represented by primary key 3 and 7.
Businesses:
| id | name |
| 3 | Planet Express |
| 7 | Planet Express |
Lots of other tables foreign key to this business table. (Employees, etc)
Employees:
| id | name | business |
| 999 | Fry | 3 |
| 666 | Bender | 7 |
Fry and Bender really do work for the same business. I'd like to delete the business row with primary key 7, and tell all the foreign key tables that instead of cascade delete, or cascade null, they should update to point to primary key 3.
Obviously I could do this by hand, but I'd rather make the database do it on my behalf since it has a better memory for this kind of thing. Is there a way to do this? Like DELETE FROM business WHERE id = 7 ON CASCADE set 3;
or something?
Best Answer
Probably not interesting for the OP but I'll give it a shot anyhow. Given the current tables:
To fix the immediate problem we can update a join against a derived table like:
and then delete the duplicate business. Note that I assume that there are exactly two rows involved in each duplicate.
For the long run I would suggest several things. Names like id, name, etc are to vague, name them something like business_id and use that name throughout the model.
If the name of a Businesses is supposed to be unique, add a constraint that ensures this. Something like: