PostgreSQL 9.5于2016年1月7日正式发布,其中包含新特性“UPSERT”(INSERT, ON CONFLICT UPDATE), 即:插入数据,正常时写入,主键冲突时更新。 Oracle的 merge into 转化为 -- > PostgreSQL 的 INSERT INTO ... ON CONFLICT .. update... 下面简单测试下: 一、Oracle数据库: 1、创建表t1、t2,插入数据: SQL> create table t1 (id int constraint idx_t1_id primary key,name varchar(20) constraint con_name not null); Table created. SQL> insert into t1 values(1,'jason'); 1 row created. SQL> insert into t1 values(2,'lucy'); 1 row created. SQL> commit; Commit complete. SQL> create table t2 (id int constraint idx_t2_id primary key,name varchar(20) constraint con_t2_name not null); Table created. SQL> SQL> insert into t2 values(1,'jason'); insert into t2 values(3,'jack'); insert into t2 values(4,'david'); insert into t2 values(5,'jacy'); insert into t2 values(6,'switty'); 1 row created. SQL> 1 row created. SQL> 1 row created. SQL> 1 row created. SQL> 1 row created. SQL> SQL> commit; Commit complete. SQL> select * from t1; ID NAME ---------- -------------------- 1 jason 2 lucy SQL> select * from t2; ID NAME ---------- -------------------- 1 jason 3 jack 4 david 5 jacy 6 switty 2、merge into 将表t2数据合并到t1中 SQL> MERGE INTO t1 USING (SELECT id,name FROM t2) t2 ON ( t1.id=t2.id) WHEN MATCHED THEN UPDATE SET T1.name= t2.name WHEN NOT MATCHED THEN INSERT (id,name) VALUES (t2.id,t2.name); 5 rows merged. SQL> select * from t1; ID NAME ---------- -------------------- 1 jason 2 lucy 3 jack 4 david 5 jacy 6 switty 6 rows selected. SQL> 二、PostgreSQL 数据库: 1、创建表t1、t2 : postgres=# create table t1 (id int constraint idx_t1_id primary key,name varchar(20) constraint con_name not null); CREATE TABLE postgres=# postgres=# postgres=# \d t1 Table "public.t1" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- id | integer | | not null | name | character varying(20) | | not null | Indexes: "idx_t1_id" PRIMARY KEY, btree (id) postgres=# insert into t1 values(1,'jason'); INSERT 0 1 postgres=# insert into t1 values(2,'lucy'); INSERT 0 1 postgres=# select * from t1; id | name ----+------- 1 | jason 2 | lucy (2 rows) postgres=# create table t2 (id int constraint idx_t2_id primary key,name varchar(20) constraint con_t2_name not null); CREATE TABLE postgres=# insert into t2 values(1,'jason'); INSERT 0 1 postgres=# insert into t2 values(3,'jack'); INSERT 0 1 postgres=# insert into t2 values(4,'david'); INSERT 0 1 postgres=# insert into t2 values(5,'jacy'); INSERT 0 1 postgres=# insert into t2 values(6,'switty'); INSERT 0 1 postgres=# postgres=# postgres=# select * from t2; id | name ----+-------- 1 | jason 3 | jack 4 | david 5 | jacy 6 | switty (5 rows) 2、使用insert into ... ON CONFLICT do UPDATE ... postgres=# insert into t1 select id,name from t2 ON CONFLICT(id) do update set name=excluded.name where t1.id=excluded.id; INSERT 0 5 postgres=# select * from t1; id | name ----+-------- 2 | lucy 1 | jason 3 | jack 4 | david 5 | jacy 6 | switty (6 rows) 附: ------------------------------------------------------------------------------- 当有主键冲突时,也可以选择do nothing postgres=# insert into t1 select id,name from t2 ON CONFLICT(id) do update set name=excluded.name where t1.id=excluded.id; INSERT 0 5 postgres=# insert into t1 select id,name from t2 ON CONFLICT(id) do nothing; INSERT 0 0 postgres=#
Oracle merge 与 PG新特性 UPSERT
来源:这里教程网
时间:2026-03-14 19:31:11
作者:
编辑推荐:
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- PostgreSQL 11 tpcc 测试(103万tpmC on ECS) - use sysbench-tpcc by Percona-Lab
- PostgreSQL PostGIS point join polygon (by ST_xxxx) - pglz_decompress 性能优化
- 掌握后台任务管理(RockyLinux jobs命令详解)
掌握后台任务管理(RockyLinux jobs命令详解)
26-03-14 - PostgreSQL DBA(9) - 执行计划数据结构
PostgreSQL DBA(9) - 执行计划数据结构
26-03-14 - PostgreSQL DBA(6) - PG 11 New Features#3
- PostgreSQL 源码解读(27)- 查询语句#12(查询优化-上拉子链接#2)
- PostgreSQL 源码解读(28)- 查询语句#13(查询优化-上拉子链接#3)
- PostgreSQL DBA(3) - 日志分析工具pgbadger简介
PostgreSQL DBA(3) - 日志分析工具pgbadger简介
26-03-14 - PostgreSQL 源码解读(29)- 查询语句#14(查询优化-上拉子查询)
- PostgreSQL 源码解读(30)- 查询语句#15(查询优化-扁平化处理UNION ALL)
天极热推
-
PostgreSQL 11 tpcc 测试(103万tpmC on ECS) - use sysbench-tpcc by Percona-Lab
查看
-
PostgreSQL PostGIS point join polygon (by ST_xxxx) - pglz_decompress 性能优化
查看
-
掌握后台任务管理(RockyLinux jobs命令详解)
查看
-
PostgreSQL DBA(9) - 执行计划数据结构
查看
-
PostgreSQL DBA(6) - PG 11 New Features#3
查看
-
PostgreSQL 源码解读(27)- 查询语句#12(查询优化-上拉子链接#2)
查看
