您好,登錄后才能下訂單哦!
怎么在PostgreSQL中實現一個upsert功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
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");
數據存在則更新數據,不存在則插入數據
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為必須為唯一主鍵,也可以用下面寫法(注意:必須保證篩選出數據唯一)
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'
上面的兩種的寫法,是先執行insert如果主鍵沖突則執行update,沒有沖突就執行insert了。要是想先執行update語句呢?
update更新失敗執行insert,更新成功則執行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' )
補充:Postgresql插入或更新操作upsert
冪等性的一個要求是多次操作的結果一致。對于update操作,多次直接的結果都是最后update的值,是滿足需求的。但對于insert,如果已經插入,第二次會報錯,duplicate error, 主鍵重復或者unique key duplicate。所以需要做一下處理。
最簡單的就是,try-catch,當報錯的時候,調用update去更新,或者策略更簡單點,直接返回就行,不需要更新,以第一條為準。
PostgreSQL從9.5之后就提供了原子的upsert語法: 不存在則插入,發生沖突可以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.
創建表
CREATE TABLE "test"."upsert_test" ( "id" int4 NOT NULL, "name" varchar(255) COLLATE "pg_catalog"."default" ) ;
當主鍵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 代指要插入的記錄
當主鍵或者unique key發生沖突時,什么都不做
INSERT INTO test.upsert_test(id, "name") VALUES(1, 'm'),(2, 'n'),(4, 'c') ON conflict(id) DO NOTHING;
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。