建表语句:
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。
Inert语法
官方文档: 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;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]