2012-08-07 72 views
0

我接管了使用mysql innodb数据库的Web应用程序的开发。客户端在事故中有很多重复记录。我正在构建一个合并工具来保存一条记录并将其他相关数据推送给它。如何在innodb表中合并2行

因此,例如由于错字,我可能会有。

entity_id entity_cat common_name 
-------------------------------------- 
abcdefg  customer  John Doe 
hijklmn  customer  Jon Doe 

然后我有一堆链接到entity_id的表。我想删除'hijklmn',并让其他表中的所有关联数据将其'customer_entity_id'更改为'abcdef'。

问题是,关系都在删除级联。我有没有办法合并这两条记录而不丢失任何数据?

+0

这会有所帮助: http://stackoverflow.com/a/10734643/613247 – Edmon 2012-08-07 17:55:36

回答

0

至于你ON DELETE CASCADE的问题,您可以暂时禁用所有的外键约束查询的目的与下面的脚本模板:

ALTER TABLE `entities` NOCHECK CONSTRAINT ALL 

-- commit query here 

ALTER TABLE `entities` CHECK CONSTRAINT ALL 
+0

不行!我要去测试一下。谢谢!在我尝试完成后,我会将其标记为已回答。 – nothappybob 2012-08-07 17:57:24

0

如果你更新所有的记录你做删除前,那么不应该有在所有任何级联:

update other_table_1 set entity_id='abcdefg' where entity_id='hijklmn'; 
update other_table_2 set entity_id='abcdefg' where entity_id='hijklmn'; 
update other_table_3 set entity_id='abcdefg' where entity_id='hijklmn'; 
... 
delete from user_table where entity_id='abcdefg' 
+0

我试过了,但是我得到了外键错误。它们在更新上级联。 – nothappybob 2012-08-07 18:22:42