2012-08-07 35 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