您好,登錄后才能下訂單哦!
怎么在PostgreSQL中實(shí)現(xiàn)一個upsert功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
DROP TABLE IF EXISTS "goods"; CREATE TABLE "goods" ( "store_cd" int4 NOT NULL, "good_cd" varchar(50) COLLATE "pg_catalog"."default" NOT NULL, "name" varchar(255) COLLATE "pg_catalog"."default" ); INSERT INTO "goods" VALUES (101, '1', '張三'); INSERT INTO "goods" VALUES (102, '2', '李四'); INSERT INTO "goods" VALUES (103, '3', '王五'); ALTER TABLE "goods" ADD CONSTRAINT "pr_cd_key" PRIMARY KEY ("store_cd", "good_cd");
數(shù)據(jù)存在則更新數(shù)據(jù),不存在則插入數(shù)據(jù)
INSERT INTO GOODS VALUES ( 104, '4', '趙六' ) ON CONFLICT ON CONSTRAINT pr_key_cd DO UPDATE SET NAME = '更新' WHERE GOODS.STORE_CD = '104' AND GOODS.GOOD_CD = '4'
pr_key_cd為必須為唯一主鍵,也可以用下面寫法(注意:必須保證篩選出數(shù)據(jù)唯一)
INSERT INTO GOODS VALUES ( 104, '4', '趙六' ) ON CONFLICT ( STORE_CD, GOOD_CD ) DO UPDATE SET NAME = '更新' WHERE GOODS.STORE_CD = '104' AND GOODS.GOOD_CD = '4'
上面的兩種的寫法,是先執(zhí)行insert如果主鍵沖突則執(zhí)行update,沒有沖突就執(zhí)行insert了。要是想先執(zhí)行update語句呢?
update更新失敗執(zhí)行insert,更新成功則執(zhí)行update。
WITH TABLE1 AS ( UPDATE GOODS SET NAME = '更新' WHERE STORE_CD = '104' AND GOOD_CD = '4' RETURNING * ) INSERT INTO GOODS SELECT 104, '4', '趙六' WHERE NOT EXISTS ( SELECT 1 FROM TABLE1 WHERE STORE_CD = '104' AND GOOD_CD = '4' )
補(bǔ)充:Postgresql插入或更新操作upsert
冪等性的一個要求是多次操作的結(jié)果一致。對于update操作,多次直接的結(jié)果都是最后update的值,是滿足需求的。但對于insert,如果已經(jīng)插入,第二次會報錯,duplicate error, 主鍵重復(fù)或者unique key duplicate。所以需要做一下處理。
最簡單的就是,try-catch,當(dāng)報錯的時候,調(diào)用update去更新,或者策略更簡單點(diǎn),直接返回就行,不需要更新,以第一條為準(zhǔn)。
PostgreSQL從9.5之后就提供了原子的upsert語法: 不存在則插入,發(fā)生沖突可以update。
官方文檔: https://www.postgresql.org/docs/devel/sql-insert.html
[ WITH [ RECURSIVE ] with_query [, ...] ] INSERT INTO table_name [ AS alias ] [ ( column_name [, ...] ) ] [ OVERRIDING { SYSTEM | USER} VALUE ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query } [ ON CONFLICT [ conflict_target ] conflict_action ] [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ] where conflict_target can be one of: ( { index_column_name | ( index_expression ) } [ COLLATE collation ] [ opclass ] [, ...] ) [ WHERE index_predicate ] ON CONSTRAINT constraint_name and conflict_action is one of: DO NOTHING DO UPDATE SET { column_name = { expression | DEFAULT } | ( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) | ( column_name [, ...] ) = ( sub-SELECT ) } [, ...] [ WHERE condition ]
index_column_name
The name of a table_name column. Used to infer arbiter indexes. Follows CREATE INDEX format. SELECT privilege on index_column_name is required.
index_expression
Similar to index_column_name, but used to infer expressions on table_name columns appearing within index definitions (not simple columns). Follows CREATE INDEX format. SELECT privilege on any column appearing within index_expression is required.
創(chuàng)建表
CREATE TABLE "test"."upsert_test" ( "id" int4 NOT NULL, "name" varchar(255) COLLATE "pg_catalog"."default" ) ;
當(dāng)主鍵id沖突時,更新其他字段
INSERT INTO test.upsert_test(id, "name") VALUES(1, 'm'),(2, 'n'),(4, 'c') ON conflict(id) DO UPDATE SET "name" = excluded.name;
did 沖突的主鍵
EXCLUDED 代指要插入的記錄
當(dāng)主鍵或者unique key發(fā)生沖突時,什么都不做
INSERT INTO test.upsert_test(id, "name") VALUES(1, 'm'),(2, 'n'),(4, 'c') ON conflict(id) DO NOTHING;
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。
億速云公眾號
手機(jī)網(wǎng)站二維碼
Copyright ? Yisu Cloud Ltd. All Rights Reserved. 2018 版權(quán)所有
廣州億速云計算有限公司粵ICP備17096448號-1 粵公網(wǎng)安備 44010402001142號增值電信業(yè)務(wù)經(jīng)營許可證編號:B1-20181529