Oracle兩種暫存資料表的建立與使用詳解,oracle兩種使用詳解
ORACLE資料庫除了可以儲存永久表外,還可以建立暫存資料表temporary tables。這些暫存資料表用來儲存一個會話SESSION的資料,或者儲存在一個事務中需要的資料。當會話退出或者使用者提交commit和復原rollback事務的時候,暫存資料表的資料自動清空,但是暫存資料表的結構以及中繼資料還儲存在使用者的資料字典中。
分類:
1.會話級暫存資料表
會話級暫存資料表是指暫存資料表中的資料只在會話生命週期之中存在,當使用者退出會話結束的時候,Oracle自動清除暫存資料表中資料。
格式:
Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Preserve Rows;
2.事務級暫存資料表
事務級暫存資料表是指暫存資料表中的資料只在事務生命週期中存在。
Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Delete Rows;
當一個事務結束(commit or rollback),Oracle自動清除暫存資料表中資料。
下面在Oracle 10g中示範了暫存資料表的建立與使用:
1.建立事務級暫存資料表,插入一條資料,並查詢:
create global temporary table transaction_temp_tb (col1 varchar(20)) on commit delete rows;insert into transaction_temp_tb values('test');select * from transaction_temp_tb;
2.執行commit或者rollback操作,暫存資料表內資料就被清空,但表結構依然存在:
3.建立一個會話級暫存資料表,插入一條資料,並查詢:
create global temporary table session_temp_tb (col1 varchar(20)) on commit preserve rows;insert into session_temp_tb values('test');select * from session_temp_tb;
4..執行commit或者rollback操作,表內資料依然存在,建立一個命令視窗(相當於開啟了一個新的會話),表內的資料就查詢不到了:
5.如果建立會話暫存資料表的會話沒有結束,則無法刪除此暫存資料表,因為暫存資料表,還在使用之中,但是結束會話(關閉建立會話級暫存資料表的命令視窗)後就可以刪除了: