oracle数据库写临时表,Oracle数据库开发——临时表

关于临时表,在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博客 ” ,链接:http://blog.itpub.net/28929558/viewspace-1148357/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值