在oracle中,臨時表分爲會話級別(session)和事務級別(transaction)兩種。session
會話級的臨時表在整個會話期間都存在,直到會話結束;事務級別的臨時表數據在transaction結束後消失,即commit/rollback或結束會話時,會清除臨時表數據。oracle
一、事務級臨時表 on commit delete rows; 當COMMIT的時候刪除數據(默認狀況)
二、會話級臨時表 on commit preserve rows; 當COMMIT的時候保留數據,當會話結束刪除數據url
1.會話級別臨時表.net
會話級臨時表是指臨時表中的數據只在會話生命週期之中存在,當用戶退出會話結束的時候,Oracle自動清除臨時表中數據。生命週期
建立方式1:事務
create global temporary table temp1(id number) on commit PRESERVE rows;get
insert into temp1values(100);it
select * from temp1;io
建立方式2:table
create global temporary table temp1 ON COMMIT PRESERVE ROWS as select id from 另外一個表;
select * from temp1;
這個時候,在當前會話查詢數據就能夠查詢到了,可是再新開一個會話窗口查詢,就會發現temp1是空表。
2.事務級別的臨時表
建立方式1:
create global temporary table temp2(id number) on commit delete rows;
insert into temp2 values(200);
select * from temp2;
建立方式2:
create global temporary table temp2 as select id from 另外一個表;(默認建立的就是事務級別的)
select * from temp2;
這時當你執行了commit和rollback操做的話,再次查詢表內的數據就查不到了。
3.oracle的臨時表建立完就是真實存在的,無需每次都建立。
若要刪除臨時表能夠:
truncate table 臨時表名; drop table 臨時表名;