全局临时表包括:
基于会话的全局临时表(commit preserve rows)
基于事务的全局临时表(on commit delete rows)
注意:实验在两个不同的会话做
(1)创建表
//创建临时会话表
LISN@orcl>create global temporary table tmp_session1 on commit preserve rows as select * from dba_objects where 1=0;
LISN@orcl>select a.name,b.value from v$statname a join v$mystat b on a.statistic#=b.statistic# where a.name='redo size';
NAME VALUE
---------------------------------------------------------------- ----------
redo size 100496
另一个会话
//创建事务临时表
LISN@orcl>create global temporary table tmp_trans1 on commit delete rows as select * from dba_objects where 1=0;
LISN@orcl>select a.name,b.value from v$statname a join v$mystat b on a.statistic#=b.statistic# where a.name='redo size';
NAME VALUE
---------------------------------------------------------------- ----------
redo size 21912
(2)插入数据
//向临时会话表插入数据
LISN@orcl>insert into tmp_session1 select * from dba_objects;
LISN@orcl>select a.name,b.value from v$statname a join v$mystat b on a.statistic#=b.statistic# where a.name='redo size';
NAME VALUE
---------------------------------------------------------------- ----------
redo size 512820
在临时事务会话
//向临时事务表插入数据
LISN@orcl>insert into tmp_trans1 select * from dba_objects;
LISN@orcl>select a.name,b.value from v$statname a join v$mystat b on a.statistic#=b.statistic# where a.name='redo size';
NAME VALUE
---------------------------------------------------------------- ----------
redo size 434236
(3)测试
//在事务上那个会话上
LISN@orcl>select count(*) from tmp_trans;
COUNT(*)
----------
72539 --注意有数据
LISN@orcl>commit;
commit; --提交一下
LISN@orcl>select count(*) from tmp_trans; --注意提交后没数据
COUNT(*)
----------
0
//在会话上
LISN@orcl>select count(*) from tmp_session1; --注意有数据
select count(*) from tmp_session1;
COUNT(*)
----------
72537
LISN@orcl>commit; --提交一下
commit;
Commit complete.
LISN@orcl>select count(*) from tmp_session1; --依然有数据
select count(*) from tmp_session1;
COUNT(*)
----------
72537
SYS@orcl>conn lisn/lisn --quit,重新链接
conn lisn/lisn
Connected.
Session altered.
LISN@orcl>select count(*) from tmp_session1; --查询无数据
select count(*) from tmp_session1;
COUNT(*)
----------
基于会话的临时表:
全局临时表特征:
(1)基于会话的全局临时表特征:基于会话的全局临时表则是session链接退出后,临时记录自动删除,无需手动去操作,针对不同session访问全局临时表,看到的结果是不同。
(2)基于事务的全局临时表在commit或者session链接退出后,临时表记录自动删除。
1.创建表