创建和使用临时表

======================================================会话1============================================
在第一个会话创建临时表,插入数据


SQL> create global temporary table tmp_emps on commit preserve rows as 
  2  select * from employees where 1=2;


Table created.


SQL> select * from tmp_emps;


no rows selected


SQL> insert into tmp_emps select * from employees where department_id=30;


6 rows created.


SQL> commit;




====================================================会话2============================================
在第二个会话中查不到会话1中插入的数据,在会话2中临时表插入数据


SQL> select count(*) from tmp_emps;


  COUNT(*)
----------
         0


SQL> insert into tmp_emps select * from employees where department_id=50;


45 rows created.


SQL> commit;


Commit complete.


====================================================会话1=========================================


会话1中将表截断
SQL> truncate table tmp_emps;


Table truncated.
 
SQL> select * from tmp_emps;


no rows selected




====================================================会话2========================================


如果会话2不断开,则不影响会话2中temp表中的数据
SQL> select count(*) from tmp_emps;


  COUNT(*)
----------
        45


断开重连之后,会话2中临时表的数据将丢失
SQL> disconnect;
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> conn hr/oracle
Connected.


SQL> select count(*) from tmp_emps;


  COUNT(*)
----------
         0

在会话2中删除表
SQL> drop table tmp_emps;


Table dropped.


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


====================================================会话2========================================
会话1即使不断开重连,也无法找到表
SQL> select * from tmp_emps;
select * from tmp_emps
              *
ERROR at line 1:
ORA-00942: table or view does not exist
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值