Oracle merge 與 PG新特性 UPSERT
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=#
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27126919/viewspace-2217501/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle 21c新特性預覽與日常管理相關的幾個新特性Oracle
- Oracle實驗8--Merge與歸檔Oracle
- Oracle 12c新特性--ASMFD(ASM Filter Driver)特性OracleASMFilter
- lightdb -- merge into insert 相容 OracleOracle
- ORACLE sql merge into update where條件位置與效能消耗OracleSQL
- Oracle 12C新特性-History命令Oracle
- Oracle 12C新特性In-MemoryOracle
- Oracle 12c 兩個新特性Oracle
- 1 Oracle Database 19c 新特性OracleDatabase
- Oracle12C新特性_DDL日誌Oracle
- Oracle 20C 多租戶_新特性Oracle
- LightDB 23.1相容Oracle新特性支援Oracle
- Oracle MYSQL PG體系OracleMySql
- Oracle MySQL PG選型OracleMySql
- Oracle MySQL PG主從OracleMySql
- Oracle 18c新特性詳解 - 表和表空間相關的新特性Oracle
- pg12 新特性,max_wal_senders 從 max_connections 分離
- Oracle 12.2 新特性: Online PDB relocate (PDB hot move)Oracle
- oracle 19C新特性——混合分割槽表Oracle
- 【DATAGUARD】Oracle19c dataguard新特性及部署Oracle
- Oracle 12C新特性-RMAN恢復表Oracle
- Oracle 11g DG新特性--Automatic block repairOracleBloCAI
- Oracle 12c新特性---Rapid Home Provisioning (RHP)OracleAPI
- LightDB 22.4 新特性之相容Oracle sqluldr2OracleSQL
- Oracle:19c 新特性——Memoptimized Rowstore 簡介OracleZed
- LightDB23.1新特性支援Oracle hint增強DB2Oracle
- Oracle 12C新特性-資料泵新引數(LOGTIME)Oracle
- Oracle vs PG 索引資訊Oracle索引
- Oracle批次生成Merge指令碼程式Oracle指令碼
- Oracle12C新特性_不可見欄位(二)Oracle
- Oracle 18C新特性之PDB snapshot(快照) CarouselOracle
- oracle 21c 新特性之 CHECKSUM 分析函式Oracle函式
- LightDB 22.4 新特性之相容Oracle樹形查詢Oracle
- ORACLE 19c 新特性之混合分割槽表Oracle
- Oracle 12C新特性-資料泵新引數(VIEWS_AS_TABLES)OracleView
- WWDC 2018:Safari與WebKit的新特性WebKit
- Oracle 18c新特性詳解:In-Memory 專題Oracle
- 新特性:/dev/shm對Oracle 11g的影響devOracle