Oracle建立臨時表的語法

2022-04-08 20:32:10 字數 3383 閱讀 5752

在oracle8i或以上版本中,可以建立以下兩種臨時表:

1。會話特有的臨時表

create global temporary ()

on commit preserve rows;

2。事務特有的臨時表  www.2cto.com  

create global temporary ()

on commit delete rows;

create global temporary table mytemptable

所建的臨時表雖然是存在的,但是你試一下insert 一條記錄然後用別的連線登上去select,記錄是空的,明白了吧,我把下面兩句話再貼一下:

--on commit delete rows 說明臨時表是事務指定,每次提交後oracle將截斷表(刪除全部行)

--on commit preserve rows 說明臨時表是會話指定,當中斷會話時oracle將截斷表。

衝突的問題更本不用考慮.

臨時表只是儲存當前會話(session)用到的資料,資料只在事務或會話期間存在。

通過create global temporary table命令建立乙個臨時表,對於事務型別的臨時表,

資料只是在事務期間存在,對於會話型別的臨時表,資料在會話期間存在。

會話的資料對於當前會話私有。每個會話只能看到並修改自己的資料。dml鎖不會加到

臨時表的資料上。下面的語句控制行的存在性。  www.2cto.com  

● on commit delete rows 表名行只是在事務期間可見

● on commit preserve rows 表名行在整個會話期間可見

可以對臨時表建立索引,檢視,出發器,可以用export和import工具匯入匯出表的

定義,但是不能匯出資料。表的定義對所有的會話可見。

temporary tables臨時表

1簡介oracle資料庫除了可以儲存永久表外,還可以建立臨時表temporary tables。這些臨時表用來儲存乙個會話session的資料,

或者儲存在乙個事務中需要的資料。當會話退出或者使用者提交commit和回滾rollback事務的時候,臨時表的資料自動清空,

但是臨時表的結構以及元資料還儲存在使用者的資料字典中。

臨時表只在oracle8i以及以上產品中支援。

2詳細介紹

oracle臨時表分為 會話級臨時表和事務級臨時表。

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

事務級臨時表是指臨時表中的資料只在事務生命週期中存在。當乙個事務結束(commit or rollback),oracle自動清除臨時表中資料。

臨時表中的資料只對當前session有效,每個session都有自己的臨時資料,並且不能訪問其它session的臨時表中的資料。因此,

臨時表不需要dml鎖.當乙個會話結束(使用者正常退出 使用者不正常退出 oracle例項崩潰)或者乙個事務結束的時候,oracle對這個會話的

表執行 truncate 語句清空臨時表資料.但不會清空其它會話臨時表中的資料.

你可以索引臨時表和在臨時表基礎上建立檢視.同樣,建立在臨時表上的索引也是臨時的,也是只對當前會話或者事務有效. 

臨時表可以擁有觸發器.

3建立臨時表

臨時表的定義對所有會話session都是可見的,但是表中的資料只對當前的會話或者事務有效.

建立方法:  www.2cto.com  

1) on commit delete rows 定義了建立事務級臨時表的方法.

create global temporary table admin_work_area

(startdate date,

enddate date,

class char(20))

on commit delete rows;

example:

sql> create global temporary table admin_work_area

2  (startdate date,

3  enddate date,

4  class char(20))

5  on commit delete rows;

sql> create table permernate( a number);

sql> insert into admin_work_area values(sysdate,sysdate,'temperary table');

sql> insert into permernate values(1);

sql> commit;

sql> select * from admin_work_area;

sql> select  * from permernate;a1

2)on commit preserve rows 定義了建立會話級臨時表的方法.

create global temporary table admin_work_area

(startdate date,

enddate date,

class char(20))

on commit preserve rows;

example:

會話1:

sql> drop table admin_work_area;

sql> create global temporary table admin_work_area

2  (startdate date,

3  enddate date,

4  class char(20))

5  on commit preserve rows;

sql> insert into permernate values(2);

sql> insert into admin_work_area values(sysdate,sysdate,'session temperary');

sql> commit;

sql> select * from permernate; a

----------12

sql> select * from admin_work_area;

startdate  enddate  class

---------- ---------- --------------------

17-1?? -03 17-1?? -03 session temperary

www.2cto.com  

會話2:

sql> select * from permernate; a

----------12

sql> select * from admin_work_area;

未選擇行.

會話2看不見會話1中臨時表的資料.

Oracle 建立臨時表

oracle 儲存過程中不像sqlserver 中 在儲存過程中用 就可以建立臨時表,用來快取需要處理的資料。oracle主要需要物理建立臨時表結構,用到global temporary table 關鍵字。語法如下 create table create global temporary tabl...

Oracle 建立臨時表

分類 oracle 22 在oracle8i或以上版本中,可以建立以下兩種臨時表 1。會話特有的臨時表 create global temporary on commit preserve rows 2。事務特有的臨時表 create global temporary on commit delet...

oracle建立表的語法

建立表的語法 建立 語法 create table 表名 欄位名1 字段型別 長度 是否為空,欄位名2 字段型別 是否為空 增加主鍵 alter table 表名 add constraint 主鍵名 primary key 欄位名1 增加外來鍵 alter table 表名 add constra...