OceanBase v4.2 特性解析:新增三种临时表功能,更多的Oracle语句兼容

特性说明

在Oracle模式下,OceanBase临时表已经实现了基本的create、select、insert、delete、update等功能。为了满足更多客户的需求,OceanBase正在扩展临时表的功能,例如支持merge into和insert all操作。merge into允许用户将源表中的数据行以更新或插入的方式合并到目标表中,而insert all则允许用户一次性将数据插入到多个目标表中。

在OceanBase 4.2.2 版本中,OceanBase对merge into、insert all临时表 的功能进行了支持,同时还支持了insert、update、delete包含临时表的视图。下文详细介绍这三种使用场景。

使用场景

merge into临时表

临时表可作为merge into的目标表、源表,若merge into的目标表、源表是视图,视图中也可包含临时表。

下面对功能进行举例说明。

首先创建临时表temp1和非临时表t1。

复制代码
create global temporary table temp1(c1 int,c2 int) on commit preserve rows;
create table t1(c1 int, c2 int);

然后向temp1和t1中插入一些数据。

复制代码
insert into temp1 values(1,1),(2,2),(5,5);
insert into t1 values(1,2),(2,4),(3,6);

使用t1作为源表,temp1作为目标表,做merge into操作,将t1表中的数据和temp1表中的数据进行匹配,对匹配成功的temp1表数据进行更新,将匹配不成功的t1表数据插入temp1表中。

复制代码
merge into temp1 using t1 on (temp1.c1=t1.c1)
	when matched then update set temp1.c2=t1.c2
	when not matched then insert (c1,c2) values (t1.c1,t1.c2);

最后查看merge into临时表的结果,如下。

复制代码
select * from temp1;
+------+------+
| C1   | C2   |
+------+------+
|    1 |    2 |
|    2 |    4 |
|    5 |    5 |
|    3 |    6 |
+------+------+

insert all临时表

临时表可作为insert all的目标表、源表。

首先创建临时表temp1和非临时表t1, t2。

复制代码
create global temporary table temp1(c1 int,c2 int) on commit preserve rows;
create table t1(c1 int, c2 int);
create table t2(c1 int, c2 int);

然后向temp1, t1, t2中插入一些数据。

复制代码
insert into temp1 values(1,1),(2,2);
insert into t1 values(1,2),(2,4);
insert into t2 values(3,5);

使用t2作为源表,temp1和t1作为目标表,做insert all操作,将t2表中的数据插入temp1和t1表中。

复制代码
insert all
	into temp1 (c1, c2) values (v1, v2)
  into t1 (c1, c2) values (v1, v2)
	select c1 as v1, c2 as v2 from t2;

最后查看insert all临时表的结果,如下。

复制代码
select * from temp1;
+------+------+
| C1   | C2   |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    3 |    5 |
+------+------+

select * from t1;
+------+------+
| C1   | C2   |
+------+------+
|    1 |    2 |
|    2 |    4 |
|    3 |    5 |
+------+------+

insert、update、delete包含临时表的视图

可以对包含临时表的视图进行insert、update、delete操作。

首先创建临时表temp1和包含临时表的视图v1。

复制代码
create global temporary table temp1(c1 int,c2 int) on commit preserve rows;
create view v1 as select * from temp1;

对视图进行insert操作。

复制代码
insert into v1 values (3,4);
insert into v1 values (4,5);

查看insert的结果,如下。

复制代码
select * from temp1;
+------+------+
| C1   | C2   |
+------+------+
|    3 |    4 |
|    4 |    5 |
+------+------+

select * from v1;
+------+------+
| C1   | C2   |
+------+------+
|    3 |    4 |
|    4 |    5 |
+------+------+

对视图进行update操作。

复制代码
update v1 set c1 = 5 where c2 = 4;

查看update的结果,如下。

复制代码
select * from temp1;
+------+------+
| C1   | C2   |
+------+------+
|    5 |    4 |
|    4 |    5 |
+------+------+

select * from v1;
+------+------+
| C1   | C2   |
+------+------+
|    5 |    4 |
|    4 |    5 |
+------+------+

对视图进行delete操作。

复制代码
delete from v1 where c1 = 4;

查看delete的结果,如下。

复制代码
select * from temp1;
+------+------+
| C1   | C2   |
+------+------+
|    5 |    4 |
+------+------+

select * from v1;
+------+------+
| C1   | C2   |
+------+------+
|    5 |    4 |
+------+------+
相关推荐
OceanBase数据库官方博客11 小时前
向量检索新选择:FastGPT + OceanBase,快速构建RAG
人工智能·oceanbase·分布式数据库·向量数据库·rag
胖头鱼的鱼缸(尹海文)10 天前
数据库管理-第313期 分布式挑战单机,OceanBase单机版试玩(20250411)
数据库·分布式·oceanbase
GottdesKrieges12 天前
OceanBase企业版单机部署:obd命令行方式
数据库·oceanbase
好记忆不如烂笔头abc12 天前
oceanbase开启内存大页
linux·服务器·oceanbase
OceanBase数据库官方博客13 天前
OceanBase 推出单机版 ,为中小规模业务提供高性价比方案
oceanbase·单机数据库
OceanBase数据库官方博客14 天前
OceanBase V4.3.5 上线全文索引功能,让数据检索更高效
全文检索·oceanbase·分布式数据库·产品特性
OceanBase数据库官方博客14 天前
多点:分布式升级助力新零售转型,成本节省超80% | OceanBase 案例
oceanbase·零售·架构选型·布道师计划·电商与零售
OceanBase数据库官方博客14 天前
火山模型的优缺点与优化实践 | OceanBase SQL优化
sql·oceanbase·分布式数据库
冰 河24 天前
《Mycat核心技术》第21章:高可用负载均衡集群的实现(HAProxy + Keepalived + Mycat)
分布式·微服务·程序员·分布式数据库·mycat
韩曙亮1 个月前
【系统架构设计师】数据库系统 ② ( 分布式数据库 | 分布式数据库 特点 | 分布式数据库 分层模式 | 两阶段提交协议 - 2PC 协议 )
数据库·分布式·系统架构·分布式数据库·软考·dbms·两阶段提交协议