主頁(yè) > 知識(shí)庫(kù) > PostgreSQL的upsert實(shí)例操作(insert on conflict do)

PostgreSQL的upsert實(shí)例操作(insert on conflict do)

熱門標(biāo)簽:辦公外呼電話系統(tǒng) 漯河外呼電話系統(tǒng) 地圖標(biāo)注和圖片名稱的區(qū)別 合肥公司外呼系統(tǒng)運(yùn)營(yíng)商 外呼調(diào)研系統(tǒng) 重慶自動(dòng)外呼系統(tǒng)定制 打電話智能電銷機(jī)器人授權(quán) 美容工作室地圖標(biāo)注 海豐有多少商家沒有地圖標(biāo)注

建表語(yǔ)句:

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ù),不存在則插入數(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語(yǔ)句呢?

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

冪等性的一個(gè)要求是多次操作的結(jié)果一致。對(duì)于update操作,多次直接的結(jié)果都是最后update的值,是滿足需求的。但對(duì)于insert,如果已經(jīng)插入,第二次會(huì)報(bào)錯(cuò),duplicate error, 主鍵重復(fù)或者unique key duplicate。所以需要做一下處理。

最簡(jiǎn)單的就是,try-catch,當(dāng)報(bào)錯(cuò)的時(shí)候,調(diào)用update去更新,或者策略更簡(jiǎn)單點(diǎn),直接返回就行,不需要更新,以第一條為準(zhǔn)。

PostgreSQL從9.5之后就提供了原子的upsert語(yǔ)法: 不存在則插入,發(fā)生沖突可以u(píng)pdate。

Inert語(yǔ)法

官方文檔: 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沖突時(shí),更新其他字段

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ā)生沖突時(shí),什么都不做

INSERT INTO test.upsert_test(id, "name")
VALUES(1, 'm'),(2, 'n'),(4, 'c')
ON conflict(id) DO NOTHING;

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • postgresql 13.1 insert into select并行查詢的實(shí)現(xiàn)
  • PostgreSQL 實(shí)現(xiàn)distinct關(guān)鍵字給單獨(dú)的幾列去重
  • postgresql insert into select無法使用并行查詢的解決
  • 啟動(dòng)PostgreSQL服務(wù)器 并用pgAdmin連接操作
  • SpringBoot連接使用PostgreSql數(shù)據(jù)庫(kù)的方法
  • postgresql合并string_agg函數(shù)的實(shí)例
  • PostgreSQL時(shí)間線(timeline)和History File的用法
  • postgresql 數(shù)據(jù)庫(kù) 與TimescaleDB 時(shí)序庫(kù) join 在一起
  • PostgreSQL 對(duì)IN,EXISTS,ANY/ALL,JOIN的sql優(yōu)化方案

標(biāo)簽:錦州 來賓 株洲 蚌埠 衡陽(yáng) 珠海 烏海 晉城

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PostgreSQL的upsert實(shí)例操作(insert on conflict do)》,本文關(guān)鍵詞  PostgreSQL,的,upsert,實(shí)例,操作,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PostgreSQL的upsert實(shí)例操作(insert on conflict do)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PostgreSQL的upsert實(shí)例操作(insert on conflict do)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章