向一张表执行插入动作,如果插入的字段数据已存在,则执行更新操作,不存在则进行插入操作。
1、创建一张表
CREATE TABLE "user_info" (
"id" int2 NOT NULL,
"name" varchar(20) COLLATE "pg_catalog"."default",
CONSTRAINT "user_info_pkey" PRIMARY KEY ("id")
);
2、执行编辑语句
INSERT INTO "user_info" ("id","name") VALUES (1,'张三') ON CONFLICT ("id") DO UPDATE SET "name" = '张三';
3、执行编辑语句("id"值相同)
INSERT INTO "user_info" ("id","name") VALUES (1,'李四') ON CONFLICT ("id") DO UPDATE SET "name" = '李四';