zx@ORCL>insert into temp_transaction values (1,'zx');
1 row created.
zx@ORCL>select * from temp_transaction;
ID NAME
---------- ------------------------------
1 zx
--同时session2查询临时表并插入测试数据
SQL> select * from temp_transaction;
未选定行
SQL> insert into temp_transaction values (2,'lx');
已创建 1 行。
SQL> select * from temp_transaction;
ID NAME
---------- --------------------
2 lx
--session1 commit操作
zx@ORCL>commit;
Commit complete.
zx@ORCL>select * from temp_transaction;
no rows selected
--session2 查询数据,commit操作
SQL> select * from temp_transaction;
ID NAME
---------- --------------------
2 lx
SQL> commit;
提交完成。
SQL> select * from temp_transaction;
未选定行
由上述操作过程可以看到不同的会话对于临时表的操作是完全隔离的,commit后,基于事务的临时表数据就会“消失”。
2.基于会话的临时表
zx@ORCL>create global temporary table temp_session
2 (id number,
3 name varchar2(10) )
4 on commit preserve rows;
Table created.
zx@ORCL>insert into temp_session values (1,'zx');
1 row created.
zx@ORCL>select * from temp_session;
ID NAME
---------- ------------------------------
1 zx
zx@ORCL>commit;
Commit complete.
zx@ORCL>select * from temp_session;
ID NAME
---------- ------------------------------
1 zx
zx@ORCL>truncate table temp_session;
Table truncated.
zx@ORCL>select * from temp_session;
no rows selected
--truncate表后数据“消失”
zx@ORCL>insert into temp_session values (1,'zx');
1 row created.
zx@ORCL>commit;
Commit complete.
zx@ORCL>select * from temp_session;
ID NAME
---------- ------------------------------
1 zx
--commit后数据还在
--退出会话重新进入
zx@ORCL>exit
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
[oracle@rhel6 ~]$ zx
SQL*Plus: Release 11.2.0.4.0 Production on Sun Jan 1 22:46:13 2017
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
zx@ORCL>select * from temp_session;
no rows selected
通过上述操作可知,基于会话的临时表,commit后数据也不会“消失”,只有当truncate或退出会话后数据才会“消失”。 查看临时表的存在
zx@ORCL>col table_name for a30
zx@ORCL>col tablespace_name for a30
zx@ORCL>col DURATION for a30
zx@ORCL>select table_name,TABLESPACE_NAME,LOGGING,TEMPORARY,DURATION,MONITORING from user_tables;
TABLE_NAME TABLESPACE_NAME LOGGING TEM DURATION MONITORIN
--如果删除时报错ORA-14452: attempt to create, alter or drop an index on temporary table already in use说明有会话正在使用临时表,无法删除
参考:
http://www.cnblogs.com/kerrycode/p/3285936.html
《9I10G11G编程艺术 深入数据库体系结构》