1
我需要每天導入一個具有數千行數十行的csv文件到Postgres數據庫。我正在研究最有效的方法,因爲csv文件中的每一行都可以是新記錄,或者是現有的記錄,如果它在那裏就應該更新。許多搜索後,我偶然發現了一個解決方案,我用:Postgres規則,以幫助CSV導入
CREATE OR REPLACE RULE insert_on_duplicate_update_advertiser_campaign_keywords_table AS
ON INSERT TO advertiser_campaign_keywords
WHERE (new.phrase, new.match_type, new.advertiser_campaign_id) IN (
SELECT phrase, match_type, advertiser_campaign_id
FROM advertiser_campaign_keywords
WHERE phrase = new.phrase AND match_type = new.match_type AND advertiser_campaign_id = new.advertiser_campaign_id AND state != 'deleted')
DO INSTEAD
UPDATE advertiser_campaign_keywords
SET bid_price_cpc = new.bid_price_cpc
WHERE phrase = new.phrase AND match_type = new.match_type AND advertiser_campaign_id = new.advertiser_campaign_id;
這是我來工作的解決方案最接近的,但它是不完整的。它未能在插入看起來像這樣:
INSERT INTO advertiser_campaign_keywords (phrase, bid_price_cpc, match_type, advertiser_campaign_id) VALUES
('dollar', 1::text::money, 'Broad', 1450),
('two words', 1.2::text::money, 'Broad', 1450),
('two words', 1.0::text::money, 'Broad', 1450),
('three words exact', 2.5::text::money, 'Exact', 1450),
('four words broad match', 1.1::text::money, 'Exclusive', 1450),
('three words exact', 2.1::text::money, 'Exact', 1450);
的錯誤信息是:
duplicate key value violates unique constraint "unique_phrase_campaign_combo"
unique_phrase_campaign_combo樣子:除非爲已刪除的記錄標記
CONSTRAINT "unique_phrase_campaign_combo" UNIQUE ("phrase", "advertiser_campaign_id", "match_type", "deleted_at")
deleted_at爲空。
任何人都知道我可以如何解決這個問題?
謝謝
順便說一句 - 在MySQL中,你可以使用所提供的答案在這裏:http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql – warren 2011-02-17 12:49:20
沃倫:MySQL有插入忽略,並且postgres不 – monksy 2011-02-19 03:46:53