【SQL】全局临时表 GLOBAL TEMPORARY TABLE

本文详细介绍了Oracle中的两种临时表类型:oncommitdeleterows事务级临时表,会在事务提交时自动删除数据;以及oncommitpreserverows会话级临时表,仅在会话结束时清除。通过实例展示了它们的创建、操作和删除过程。

目录

定义

on commit delete rows 事务级临时表

on commit preserve rows 会话级临时表

删除临时表


定义

临时表是用来保留临时或者中间数据的表,属于数据哭对象,有对应编号

它可以像普通表一样使用,并为每个会话提供专有数据,会话之间不受影响

临时表的数据会在会话完成或事物结束时自动清除数据

它存在于temp表空间内并不占用用户表空间

对于临时表的操作,不生成日志和回滚数据,没有锁,不备份,不维护

 

on commit delete rows 事务级临时表

在用户提交(commit)时清除数据,也叫事务级临时表

 

由sys用户创建全局临时表

SQL> create global temporary table tb_temp01 on commit delete rows as select * from scott.salgrade;
 

Table created.
 

SQL> select * from tb_temp01;
 

no rows selected

create 属于ddl会隐式提交,因此表中没有数据

在该会话的临时表中添加数据

SQL> insert into tb_temp01 select * from scott.salgrade;
 

5 rows created.
 

SQL> select * from tb_temp01;
 
     GRADE      LOSAL      HISAL

---------- ---------- ----------
         1        700       1200
         2       1201       1400
         3       1401       2000
         4       2001       3000
         5       3001       9999

新建一个会话查看临时表中的内容

SQL> show user
USER is "SYS"
SQL> select * from tb_temp01;
 

no rows selected

看不到数据,因为临时表的数据仅能在当前会话中看到

在原来的会话中提交数据看一下

SQL> select * from tb_temp01;
 
     GRADE      LOSAL      HISAL

---------- ---------- ----------
         1        700       1200
         2       1201       1400
         3       1401       2000
         4       2001       3000
         5       3001       9999
 
SQL> commit;
 

Commit complete.
 

SQL> select * from tb_temp01;
 

no rows selected

提交数据后,反而看不到数据,这是因为 on commit delete rows的限制

 

on commit preserve rows 会话级临时表

此关键字意为当前会话结束时,丢弃数据,也叫会话级临时表

还是用sys用户建立会话集临时表

SQLcreate global temporary table tb_temp02 on commit preserve rows as select * from scott.salgrade;
 

Table created.
 

SQL> select * from tb_temp02;
 
     GRADE      LOSAL      HISAL

---------- ---------- ----------
         1        700       1200
         2       1201       1400
         3       1401       2000
         4       2001       3000
         5       3001       9999

会话级临时表在ddl之后依旧保留数据

那么在另一个会话中能不能看到数据呢

SQL> conn / as sysdba
Connected.

SQL> select * from tb_temp02;
 

no rows selected

显然不能

在前一个会话中对表进行增删改等dml操作并提交

 
SQL> insert into tb_temp02 values(9,9999,9999);
 

1 row created.
 

SQL> delete tb_temp02 where grade=1;
 

1 row deleted.
 

SQL> update tb_temp02 set grade=88 where grade=4;
 

1 row updated.
 

SQL> select * from tb_temp02;
 
     GRADE      LOSAL      HISAL

---------- ---------- ----------
         2       1201       1400
         3       1401       2000
        88       2001       3000
         5       3001       9999
         9       9999       9999
 
SQL> commit;
 

Commit complete.
 

SQL> select * from tb_temp02;
 
     GRADE      LOSAL      HISAL

---------- ---------- ----------
         2       1201       1400
         3       1401       2000
        88       2001       3000
         5       3001       9999
         9       9999       9999

数据依然存在

结束会话或切换用户之后数据消失

SQL> select * from tb_temp02;
 
     GRADE      LOSAL      HISAL

---------- ---------- ----------
         2       1201       1400
         3       1401       2000
        88       2001       3000
         5       3001       9999
         9       9999       9999
 
SQL> conn tiger/scott;
Connected.

SQL> conn / as sysdba
Connected.

SQL> select * from tb_temp02;
 

no rows selected

 

删除临时表

事物级临时表可以直接用drop语句删除

SQL> select * from tb_temp01;
 

no rows selected
 

SQL> insert into tb_temp01 select * from scott.salgrade;
 

5 rows created.
 

SQL> select * from tb_temp01;
 
     GRADE      LOSAL      HISAL

---------- ---------- ----------
         1        700       1200
         2       1201       1400
         3       1401       2000
         4       2001       3000
         5       3001       9999
 
SQL> drop table tb_temp01;
 

Table dropped.

因为drop也属于DDL操作,进行隐式提交,提交后清除数据

但是会话级临时表不可以必须要对表进行截断或者清除数据,再或者退出会话之后才能删除

SQL> select * from tb_temp02;
 

no rows selected
 

SQL> insert into tb_temp02 select * from scott.salgrade;
 

5 rows created.
 

SQL> drop table tb_temp02;
drop table tb_temp02
           *
ERROR
at line 1:
ORA-
14452: attempt to create, alter or drop an index on temporary table already
in use
 
 

SQL> commit;
 

Commit complete.
 

SQL> drop table tb_temp02;
drop table tb_temp02
           *
ERROR
at line 1:
ORA-
14452: attempt to create, alter or drop an index on temporary table already
in use
 
 

SQL> truncate table tb_temp02;
 

Table truncated.
 

SQL> delete from tb_temp02;
 

0 rows deleted.
 

SQL> drop table tb_temp02;
 

Table dropped.

总之来说临时表删除的时候表内不能有数据

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aluphami

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值