Oracle临时表

Oracle临时表

Oracle临时表有3种:事务级临时表、会话级临时表和虚拟临时表

(一)事务临时表
事务临时表的生存周期为一个事务,当用commit提交或用rollback会滚后,事务结束,表被清空,但表的结构还在。

实验:
SQL> create global temporary table tmp_dept on commit delete rows as select * from dept where 1 = 2;

SQL> insert into tmp_dept select * from dept;
4 rows created.

SQL> select * from tmp_dept;

DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON

SQL> commit;

Commit complete.

SQL> select * from tmp_dept;

no rows selected

上述no rows selected表明数据被清空,但表的结构还在。

(二)会话临时表
会话临时表的生存期为会话期,但会话结束后,表数据被清空,但表结构还在。

实验:
SQL> create global temporary table temp_dept(deptno number(2) not null, dname varchar(14), loc varchar(13)) on commit preserve rows;

SQL> insert into temp_dept select * from dept;

SQL> col deptno format 99;
SQL> col dname format a14;
SQL> col loc format a13;
SQL> select * from temp_dept;

DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON

SQL> commit;

SQL> select * from temp_dept;

DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON

上述结果说明事务结束之后,表结构和数据仍然存在。

用conn来结束当前会话,开启一个新的会话:
SQL> conn system/Zhs830613
Connected.

用conn再次结束当前会话,开启一个新的会话:
SQL> conn scott/Zhs830613
Connected.

此时,表中的数据被清空,但表的结构还在:
SQL> select * from temp_dept;

no rows selected

(三)虚拟临时表
虚拟临时表只能在一条语句中使用。

实验:
SQL> with tmp_table as (select * from dept)
select * from tmp_table;

DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON

SQL> select * from tmp_table;
select * from tmp_table
              *
ERROR at line 1:
ORA-00942: table or view does not exist


(四)总结
三种表都不能永久的保存记录。
事务级临时表、会话级临时表都是使用临时表空间;虚拟临时表连表空间都没有,全在内存里。
事务级临时表在事务结束后被清空,但表结构依然存在。
会话级临时表在会话断开后被清空,但表结构依然存在。
虚拟临时表只能在select语句中使用,并且只对一条语句使用。使用完后,表结构都没有了。若第二条语句继续使用,会报找不到对象的错误。


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

转载于:http://blog.itpub.net/29485627/viewspace-1249399/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值