ITPub博客

首页 > 数据库 > Oracle > Oracle两种临时表的创建与使用详解

Oracle两种临时表的创建与使用详解

Oracle 作者:linmenglan1 时间:2018-04-18 15:16:44 0 删除 编辑

原链接:https://www.2cto.com/database/201504/394937.html

ORACLE数据库除了可以保存永久表外,还可以建立临时表temporary tables。这些临时表用来保存一个会话SESSION的数据,或者保存在一个事务中需要的数据。当会话退出或者用户提交commit和回滚rollback事务的时候,临时表的数据自动清空,但是临时表的结构以及元数据还存储在用户的数据字典中。

分类:
1.会话级临时表

会话级临时表是指临时表中的数据只在会话生命周期之中存在,当用户退出会话结束的时候,Oracle自动清除临时表中数据。

格式:

?
1
2
3
4
5
6
7
Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Preserve Rows;

2.事务级临时表
事务级临时表是指临时表中的数据只在事务生命周期中存在。
?
1
2
3
4
5
6
7
Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Delete Rows;

当一个事务结束(commit or rollback),Oracle自动清除临时表中数据。

下面在Oracle 10g中演示了临时表的创建与使用:

1.创建事务级临时表,插入一条数据,并查询:

 

?
1
2
3
create global temporary table transaction_temp_tb (col1 varchar(20)) on commit delete rows;
insert into  transaction_temp_tb values('test');
select * from  transaction_temp_tb;
.

 

2.执行commit或者rollback操作,临时表内数据就被清空,但表结构依然存在:

.

3.创建一个会话级临时表,插入一条数据,并查询:

 

?
1
2
3
create global temporary table session_temp_tb (col1 varchar(20)) on commit preserve rows;
insert into session_temp_tb values('test');
select * from session_temp_tb;

.

 

4..执行commit或者rollback操作,表内数据依然存在,新建一个命令窗口(相当于开启了一个新的会话),表内的数据就查询不到了:

.

5.如果创建会话临时表的会话没有结束,则无法删除此临时表,因为临时表,还在使用之中,但是结束会话(关闭创建会话级临时表的命令窗口)后就可以删除了:

.

.


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

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

注册时间:2016-07-03

  • 博文量
    85
  • 访问量
    157914