• 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。

    1. create global temporary table temp1(c1 int,c2 int) on commit preserve rows;
    2. create table t1(c1 int, c2 int);

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

    1. insert into temp1 values(1,1),(2,2),(5,5);
    2. insert into t1 values(1,2),(2,4),(3,6);

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

    1. merge into temp1 using t1 on (temp1.c1=t1.c1)
    2. when matched then update set temp1.c2=t1.c2
    3. when not matched then insert (c1,c2) values (t1.c1,t1.c2);

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

    1. select * from temp1;
    2. +------+------+
    3. | C1 | C2 |
    4. +------+------+
    5. | 1 | 2 |
    6. | 2 | 4 |
    7. | 5 | 5 |
    8. | 3 | 6 |
    9. +------+------+

    insert all临时表

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

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

    1. create global temporary table temp1(c1 int,c2 int) on commit preserve rows;
    2. create table t1(c1 int, c2 int);
    3. create table t2(c1 int, c2 int);

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

    1. insert into temp1 values(1,1),(2,2);
    2. insert into t1 values(1,2),(2,4);
    3. insert into t2 values(3,5);

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

    1. insert all
    2. into temp1 (c1, c2) values (v1, v2)
    3. into t1 (c1, c2) values (v1, v2)
    4. select c1 as v1, c2 as v2 from t2;

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

    1. select * from temp1;
    2. +------+------+
    3. | C1 | C2 |
    4. +------+------+
    5. | 1 | 1 |
    6. | 2 | 2 |
    7. | 3 | 5 |
    8. +------+------+
    9. select * from t1;
    10. +------+------+
    11. | C1 | C2 |
    12. +------+------+
    13. | 1 | 2 |
    14. | 2 | 4 |
    15. | 3 | 5 |
    16. +------+------+

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

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

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

    1. create global temporary table temp1(c1 int,c2 int) on commit preserve rows;
    2. create view v1 as select * from temp1;

    对视图进行insert操作。

    1. insert into v1 values (3,4);
    2. insert into v1 values (4,5);

    查看insert的结果,如下。

    1. select * from temp1;
    2. +------+------+
    3. | C1 | C2 |
    4. +------+------+
    5. | 3 | 4 |
    6. | 4 | 5 |
    7. +------+------+
    8. select * from v1;
    9. +------+------+
    10. | C1 | C2 |
    11. +------+------+
    12. | 3 | 4 |
    13. | 4 | 5 |
    14. +------+------+

    对视图进行update操作。

    update v1 set c1 = 5 where c2 = 4;

    查看update的结果,如下。

    1. select * from temp1;
    2. +------+------+
    3. | C1 | C2 |
    4. +------+------+
    5. | 5 | 4 |
    6. | 4 | 5 |
    7. +------+------+
    8. select * from v1;
    9. +------+------+
    10. | C1 | C2 |
    11. +------+------+
    12. | 5 | 4 |
    13. | 4 | 5 |
    14. +------+------+

    对视图进行delete操作。

    delete from v1 where c1 = 4;

    查看delete的结果,如下。

    1. select * from temp1;
    2. +------+------+
    3. | C1 | C2 |
    4. +------+------+
    5. | 5 | 4 |
    6. +------+------+
    7. select * from v1;
    8. +------+------+
    9. | C1 | C2 |
    10. +------+------+
    11. | 5 | 4 |
    12. +------+------+
  • 相关阅读:
    容器内存指标
    Fief(分类讨论 + 点双连通分量v-dcc)(2022牛客暑期多校训练营3)
    windows下rust调试运行环境部署
    Springboot毕设项目古建筑信息现代数字化管理平台z6mmx(java+VUE+Mybatis+Maven+Mysql)
    每日五问(java)
    如何在PS(Adobe Photoshop)安装Portraiture3插件教程
    如何通过财务共享推进财务精细化管理
    GPT对话代码库——HAL库下 USART 的配置及问题(STM32G431CBT6)
    【无标题】
    mmdetection--pointpillars-demo运行
  • 原文地址:https://blog.csdn.net/OceanBaseGFBK/article/details/138544268