2010-10-27 63 views
3

我想使用mysqldump的輸出來更新活動數據庫中的條目。我不想首先刪除條目,簡單的更新語句很好。有沒有簡單的方法將包含INSERT語句的mysqldump的輸出轉換爲相應的UPDATE語句?自動將INSERT語句轉換爲UPDATE的簡單方法?

這似乎是一個基本特徵,所以我敢肯定有人創建了一個工具或想出了一個方法來快速完成它,所以人們不必每次都編寫自己的腳本重新發明輪子爲了這。

編輯:我正在尋找一個通用的解決方案,而不是一個我必須手動枚舉實際表列的地方。這是一個普遍的問題,所以我認爲應該有一個獨立於表格的解決方案。

回答

4

Mchl的答案是有效的另一個簡單的解決方法是將'INSERT'更改爲'REPLACE'。兩者都需要一個簡單的搜索/替換操作(我會使用sed)。但是,如果這是定期運行,那麼它將是一個很好的候選複製/使用時間戳創建加載器文件只有包含修改/新記錄。

mysldump也有一個--replace選項,所以sed的步驟可以省略。

+1

替換刪除舊行,如果它存在。它不能在實時數據庫中造成問題嗎?我不希望該行即使瞬間消失。我想更新現有的。 – Tom 2010-10-27 16:12:01

+0

這似乎是最簡單的解決方案,即使有些行會暫時消失,所以我會繼續這樣做。 – Tom 2010-10-27 17:23:06

+0

替換會導致問題,因爲如果它找到具有相同主鍵的現有行,它將刪除該行並使用相同數據創建一個新行,但會創建一個新的主鍵。在使用之前,確保你沒有這種副作用。 – siliconrockstar 2018-02-19 19:07:45

7

你可以在mysqldump的數據恢復到新的臨時數據庫,然後使用多表UPDATE語法做更新的問題。

UPDATE mydb.mytable AS dest JOIN tempdb.mytable AS origin USING (prim_key) 
SET dest.col1 = origin.col1, 
    dest.col2 = origin.col2, 
    ... 

然後刪除臨時數據庫。

+0

有趣。如果沒有其他自動解決方案將插入轉換爲更新,我可能會採用此方法。我正在尋找一種可以在任何表上使用的通用解決方案,因此我不必手動寫下列名稱。 – Tom 2010-10-27 16:13:22

+0

Upvote給你先生,這實際上是一個簡單的方法。 – 2012-03-27 03:36:33

0

我也一直在尋找一種解決方案,我認爲是一個常見問題,但一直沒能找到。看起來這將是烤到mysqldump的,但基於MySQL的板此功能的要求,它不是:

https://bugs.mysql.com/bug.php?id=73190

轉錄如下:

[3 Jul 2014 21:23] Christopher Schultz 
Description: 
mysqldump currently supports two different options for dealing with PK collisions: 

    --insert-ignore (uses INSERT IGNORE) 
    --replace  (uses REPLACE INTO instead of INSERT INTO) 

I'd like to request an additional option that uses INSERT ... ON DUPLICATE KEY UPDATE ... when used. 

This will allow one database to be used as a source to update another database without the following problems: 

Using INSERT IGNORE will ignore any updates to existing rows that are coming from the file being loaded "on top of" an existing database. 

Using REPLACE ends up churning the primary index, failing when foreign keys point to the record being updated (REPLACED), or ON DELETE CASCADE wiping-out records in other tables as the records in the target table are "updated" (using REPLACE). 

There are two significant downsides to using ON DUPLICATE KEY UPDATE, of course: 

1. The dump file will get much bigger, because the bulk-loading syntax for INSERT can no longer be used. 
2. The dump file will get much longer, because ON DUPLICATE KEY UPDATE requires that all values be specified twice: once in the VALUES() section and then a second time in the field=value, field=value part after "ON DUPLICATE KEY UPDATE" phrase. 

Bug 11422 [http://bugs.mysql.com/bug.php?id=11422] requests the ability to simply say "ON DUPLICATE KEY UPDATE" and allow the engine to use the VALUES to update all fields in the table. Fixing bug 11422 and using the solution here would mitigate both downsides because then extended syntax could (possibly?) be used and the values would not have to be mentioned twice in the dump file. 

I have found many posts online about how to do something like this and the responses always steer the person toward --insert-ignore or --replace but there are many scenarios where the above request would be preferable. 

How to repeat: 
(This is a feature request) 
[17 Jul 2015 14:23] Georgi Kodinov 
Thank you for the reasonable feature request. 
相關問題