Oracle Temporary Tables临时表

来源:互联网 发布:手机强制视频软件 编辑:程序博客网 时间:2024/06/11 03:47

Oracle临时表分为 会话级临时表 和 事务级临时表。


会话级临时表是指临时表中的数据只在会话生命周期之中存在,当用户退出会话结束的时候,Oracle自动清除临时表中数据。
事务级临时表是指临时表中的数据只在事务生命周期中存在。当一个事务结束(commit or rollback),Oracle自动清除临时表中数据。
临时表中的数据只对当前Session有效,每个Session都有自己的临时数据,并且不能访问其它Session的临时表中的数据。因此,
临时表不需要DML锁.当一个会话结束(用户正常退出 用户不正常退出 ORACLE实例崩溃)或者一个事务结束的时候,Oracle对这个会话的表执行 TRUNCATE 语句清空临时表数据.但不会清空其它会话临时表中的数据.
你可以索引临时表和在临时表基础上建立视图.同样,建立在临时表上的索引也是临时的,也是只对当前会话或者事务有效.
临时表可以拥有触发器.


建立临时表

临时表的定义对所有会话SESSION都是可见的,但是表中的数据只对当前的会话或者事务有效.

建立方法:

1) ON COMMIT DELETE ROWS 定义了建立事务级临时表的方法.
C:Documents and Settingswangfan>sqlplus wangfan/wangfan

SQL*Plus: Release 10.2.0.1.0 - Production on Fri Nov 2 16:48:31 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> create global temporary table wf(col1 varchar2(10)) on commit delete rows;
Table created.

SQL> insert into wf select * from dual connect by rownum<10;
9 rows created.
---------未提交事务前查询该表--------------------

SQL> select * from wf;
COL1
----------

X
X
X
X
X
X
X
X
X
9 rows selected.
---------提交事务后查询------------------------------
SQL> commit;
Commit complete.
SQL> select * from wf;
no rows selected
2)ON COMMIT PRESERVE ROWS 定义了创建会话级临时表的方法.
session 1:
SQL> create global temporary table wfa(col1 varchar2(10)) on commit preserve rows;
Table created.
SQL> insert into wfa select * from dual connect by rownum<10;
9 rows created.
SQL> commit;
Commit complete.
SQL> select * from wfa;
COL1
----------
X
X
X
X
X
X
X
X
X
session 2:

C:>sqlplus wangfan/wangfan
SQL*Plus: Release 10.2.0.1.0 - Production on Fri Nov 2 16:56:56 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from wfa;
no rows selected


Temporary Tables临时表
Frank, wang 2007-11-3
Oracle临时表分为 会话级临时表 和 事务级临时表。
会话级临时表是指临时表中的数据只在会话生命周期之中存在,当用户退出会话结束的时候,Oracle自动清除临时表中数据。
事务级临时表是指临时表中的数据只在事务生命周期中存在。当一个事务结束(commit or rollback),Oracle自动清除临时表中数据。
临时表中的数据只对当前Session有效,每个Session都有自己的临时数据,并且不能访问其它Session的临时表中的数据。因此,临时表不需要DML锁.当一个会话结束(用户正常退出 用户不正常退出 ORACLE实例崩溃)或者一个事务结束的时候,Oracle对这个会话的表执行 TRUNCATE 语句清空临时表数据.但不会清空其它会话临时表中的数据.
你可以索引临时表和在临时表基础上建立视图.同样,建立在临时表上的索引也是临时的,也是只对当前会话或者事务有效.
临时表可以拥有触发器.

建立临时表
临时表的定义对所有会话SESSION都是可见的,但是表中的数据只对当前的会话或者事务有效.
建立方法:
1) ON COMMIT DELETE ROWS 定义了建立事务级临时表的方法.
C:Documents and Settingswangfan>sqlplus wangfan/wangfan
SQL*Plus: Release 10.2.0.1.0 - Production on Fri Nov 2 16:48:31 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> create global temporary table wf(col1 varchar2(10)) on commit delete rows;
Table created.
SQL> insert into wf select * from dual connect by rownum<10;
9 rows created.
---------未提交事务前查询该表--------------------
SQL> select * from wf;
COL1
----------

X
X
X
X
X
X
X
X
X

9 rows selected.
---------提交事务后查询------------------------------
SQL> commit;
Commit complete.
SQL> select * from wf;
no rows selected
2)ON COMMIT PRESERVE ROWS 定义了创建会话级临时表的方法.
session 1:
SQL> create global temporary table wfa(col1 varchar2(10)) on commit preserve rows;
Table created.

SQL> insert into wfa select * from dual connect by rownum<10;
9 rows created.
SQL> commit;
Commit complete.
SQL> select * from wfa;
COL1
----------
X
X
X
X
X
X
X
X
X
session 2:
C:>sqlplus wangfan/wangfan
SQL*Plus: Release 10.2.0.1.0 - Production on Fri Nov 2 16:56:56 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select * from wfa;
no rows selected

 

原创粉丝点击