1. 程式人生 > >oracle建立臨時表

oracle建立臨時表

臨時表:像普通表一樣,有結構,但是對資料的管理上不一樣,臨時表儲存事務或會話的中間結果集,臨時表中儲存的資料只對當前 會話可見,所有會話都看不到其他會話的資料,即使其他會話提交了,也看不到。臨時表不存在併發行為,因為他們對於當前會話都是獨立的。(它預設是事務級別的)

在oracle中臨時表可分為會話級臨時表和事務級別臨時表。

臨時表的作用:對於龐大的資料我們只要查詢其中一小部分結果集這樣我們可以藉助臨時表。

1.會話級別臨時表

會話級臨時表是指臨時表中的資料只在會話生命週期之中存在,當用戶退出會話結束的時候,Oracle自動清除臨時表中資料。

create global temporary table aaa(id number) on commit oreserve rows;

insert into aaa values(100);

select * from aaa;

這是當你在開啟另一個會話視窗的時候再次查詢,表內的資料就查詢不到了。

2.事務級別的臨時表

create global temporary table bbb(id number) on commit delete rows;

insert into bbb values(200);

select * from bbb;

這時當你執行了commit和rollback操作的話,再次查詢表內的資料就查不到了。

 

預設下的:

SQL> create global temporary table aaa as select * from dba_objects;

Table created.

SQL> select * from aaa;--沒有資料,因為省掉了on commit delete rows