ITPub博客

首页 > 数据库 > > Oracle 全局临时表的小测试

Oracle 全局临时表的小测试

作者:蚂蚁没问题 时间:2019-09-06 16:00:15 0 删除 编辑

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}

临时表 -- on commit delete rows     --基于事务的临时表

1.创建临时表;

  create global temporary table temp_1 on commit delete rows as select * from dba_objects;

  

SQL>create global temporary table temp_1 on commit delete rows as select * from dba_objects;

  Table created.

创建一个基于commit提交后删除临时表中行数据的表;

  查询临时表中的数据;

2.select count(*) from temp_1;

SQL> select count(*) from temp_1;

  COUNT(*)

----------

         0

  可以看到此时的数据为零;

3.向新建的临时表中插入数据;

  insert into temp_1 select * from dba_objects;

SQL> insert into temp_1 select * from dba_objects;

72738 rows created.

4.再次查询temp_1临时表中的数据

 SQL> select count(*) from temp_1;

  COUNT(*)

----------

     72738

5.提交操作

  commit;

6.提交后的temp_1临时表中的数据被删除;

SQL> select count(*) from temp_1;

  COUNT(*)

----------

         0

临时表  on commit preserve rows  --基于会话的临时表,直至当前会话结束后才会删除临时表中的数据;

 

1.创建临时表

  create global temporary table t_tmp on commit preserve rows as select * from dba_objects;

SQL> create global temporary table t_tmp on commit preserve rows as select * from dba_objects;

Table created.

创建一个基于commit提交后删除临时表中行数据的表,但是会向先该临时表中插入数据;

2.查询刚刚建立的t_tmp临时表;

 SQL> select count(*) from t_tmp;

  COUNT(*)

----------

     72739

3.再次向该表中插入数据;

  insert into t_tmp select * from dba_objects;

SQL> insert into t_tmp select * from dba_objects;

72739 rows created.

4.提交插入的数据;

  

  commit;

5.再次查询t_tmp临时表中的数据;

 SQL> select count(*) from t_tmp;

  COUNT(*)

----------

    145478

可以看到当前会话在没有退出的时候不会删除临时表中的数据,依然会向表中追加数据;

6.退出当前会话,再次登录后查询

   exit

7.再次查询t_tmp临时表中的数据;

 SQL> select count(*) from t_tmp;

  COUNT(*)

----------

         0

可以看到,前一次会话所做对表的数据修改退出会话后数据就已经被删除.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2568/viewspace-3133470/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    107
  • 访问量
    675