分类: Oracle
2017-04-21 22:14:48
临时表 -- on commit delete rows --基于事务的临时表
1.创建临时表;
create global temporary table temp_1 on commit delete rows as select * from dba_objects;
SQL>create global temporary table temp_1 on commit delete rows as select * from dba_objects;
Table created.
创建一个基于commit提交后删除临时表中行数据的表;
查询临时表中的数据;
2.select count(*) from temp_1;
SQL> select count(*) from temp_1;
COUNT(*)
----------
0
可以看到此时的数据为零;
3.向新建的临时表中插入数据;
insert into temp_1 select * from dba_objects;
SQL> insert into temp_1 select * from dba_objects;
72738 rows created.
4.再次查询temp_1临时表中的数据
SQL> select count(*) from temp_1;
COUNT(*)
----------
72738
5.提交操作
commit;
6.提交后的temp_1临时表中的数据被删除;
SQL> select count(*) from temp_1;
COUNT(*)
----------
0
临时表 on commit preserve rows --基于会话的临时表,直至当前会话结束后才会删除临时表中的数据;
1.创建临时表
create global temporary table t_tmp on commit preserve rows as select * from dba_objects;
SQL> create global temporary table t_tmp on commit preserve rows as select * from dba_objects;
Table created.
创建一个基于commit提交后删除临时表中行数据的表,但是会向先该临时表中插入数据;
2.查询刚刚建立的t_tmp临时表;
SQL> select count(*) from t_tmp;
COUNT(*)
----------
72739
3.再次向该表中插入数据;
insert into t_tmp select * from dba_objects;
SQL> insert into t_tmp select * from dba_objects;
72739 rows created.
4.提交插入的数据;
commit;
5.再次查询t_tmp临时表中的数据;
SQL> select count(*) from t_tmp;
COUNT(*)
----------
145478
可以看到当前会话在没有退出的时候不会删除临时表中的数据,依然会向表中追加数据;
6.退出当前会话,再次登录后查询
exit
7.再次查询t_tmp临时表中的数据;
SQL> select count(*) from t_tmp;
COUNT(*)
----------
0
可以看到,前一次会话所做对表的数据修改退出会话后数据就已经被删除.