ITPub博客

首页 > 数据库 > Oracle > Oracle数据库开发——临时表

Oracle数据库开发——临时表

原创 Oracle 作者:呆呆笨笨的鱼 时间:2014-04-24 11:01:12 0 删除 编辑
关于临时表,在Oracle数据库中可以使用create global temporary table 语句来创建。
临时表创建后,其结构将一直存在,但是其中的数据在特定的条件下自动释放。
依据释放的条件不同,临时表分为 事务级临时表 和 会话级临时表。

创建语法create global temporary table() on commit {delete 事务级 | preserve 会话级} rows; 

例:创建如下3张表
--普通堆表
create table student
(s_id integer primary key
,sname varchar2(10)
);

--事务级临时表
create global temporary table student_tmp1
(s_id integer primary key
,sname varchar2(10)
)on commit delete rows;

--会话级临时表
create global temporary table student_tmp2
(s_id integer primary key
,sname varchar2(10)
)on commit preserve rows;

查看定义(临时表TABLESPACE_NAME项为空,插入数据的时候应该时候应该放到临时表空间吧,有兴趣自己去求证一下吧)
SQL> select x.TABLE_NAME,x.TABLESPACE_NAME,x.TEMPORARY from user_tables x where x.TABLE_NAME in ('STUDENT_TMP1','STUDENT_TMP2','STUDENT');
 
TABLE_NAME                     TABLESPACE_NAME                TEMPORARY
------------------------------ ------------------------------ ---------
STUDENT                        USERS                          N
STUDENT_TMP1                                                  Y
STUDENT_TMP2                                                  Y

接下来,看事务级临时表与会话级临时表的区别

SQL> insert into student_tmp1 values (12345,'criss');
 
1 row inserted
 
SQL> insert into student_tmp2 values (12345,'criss');
 
1 row inserted
 
SQL> select * from student_tmp1;
 
                                   S_ID SNAME
--------------------------------------- ----------
                                  12345 criss
 
SQL> select * from student_tmp2;
 
                                   S_ID SNAME
--------------------------------------- ----------
                                  12345 criss
                                  
未提交前,两张临时表都能查到数据,下面进行提交,再次查询

SQL> commit;
 
Commit complete
 
SQL> select * from student_tmp1;
 
                                   S_ID SNAME
--------------------------------------- ----------
 
SQL> select * from student_tmp2;
 
                                   S_ID SNAME
--------------------------------------- ----------
                                  12345 criss
                                  
此时,事务级临时表(student_tmp1)数据被清空。会话级临时表(student_tmp2)数据仍保留。

总结一下:在事务提交时,事务级临时表中的数据会被系统自动删除。
          会话级临时表在会话断开前,其中的数据不会删除,断开后清空。

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

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

注册时间:2014-04-17

  • 博文量
    62
  • 访问量
    939480