【问题标题】:How can I merge 2 rows in an innodb table如何在 innodb 表中合并 2 行
【发布时间】:2012-08-04 18:56:29
【问题描述】:

我已经接管了使用 mysql innodb 数据库的 Web 应用程序的开发。客户端意外在数据库中有很多重复记录。我正在构建一个合并工具来保留一条记录并将另一条的关联数据推送到它。

所以,例如,由于错字,我可能有。

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

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

问题是,关系都在删除级联上。有什么方法可以合并这两条记录而不丢失任何数据?

【问题讨论】:

标签: mysql sql merge duplicates innodb


【解决方案1】:

关于您的ON DELETE CASCADE 问题,您可以使用以下脚本模板暂时禁用所有外键约束以进行查询:

ALTER TABLE `entities` NOCHECK CONSTRAINT ALL

-- commit query here

ALTER TABLE `entities` CHECK CONSTRAINT ALL

【讨论】:

  • 不可能!我要去测试一下。谢谢!试用后我会将其标记为已回答。
【解决方案2】:

如果在删除之前更新所有记录,则根本不应该有任何级联:

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'

【讨论】:

  • 我试过了,但我遇到了外键错误。它们在更新时级联。
猜你喜欢
  • 1970-01-01
  • 2012-01-13
  • 2013-07-19
  • 2013-06-28
  • 2021-09-13
  • 2023-03-08
  • 2022-11-08
  • 2021-07-06
  • 1970-01-01
相关资源
最近更新 更多