暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

OceanBase v4.2.2 版本特性解读:全新三种临时表功能/应用场景揭秘

原创 OceanBase数据库 2024-05-07
640

特性说明

在Oracle模式下,OceanBase的临时表已经支持基本的create、select、insert、delete、update等功能。然而,很多客户还需要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 |
+------+------+
最后修改时间:2024-05-09 13:45:17
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论