Oracle臨時(shí)表的創(chuàng)建以及實(shí)例演示
我個(gè)人對(duì)Oracle臨時(shí)表的理解是在Oracle數(shù)據(jù)庫(kù)中創(chuàng)建一表,其主要的功能是用于自己的相關(guān)軟件系統(tǒng),如果你在用完之后就會(huì)發(fā)現(xiàn)相關(guān)表中的某些數(shù)據(jù)就沒(méi)用。Oracle的臨時(shí)表創(chuàng)建之后基本不占用表空間。
如果你沒(méi)有指定Oracle 臨時(shí)表(包括臨時(shí)表的索引)存放的表空的時(shí)候,你插入到Oracle 臨時(shí)表的數(shù)據(jù)是存放在 Oracle系統(tǒng)的臨時(shí)表空間中(TEMP)。
臨時(shí)表的創(chuàng)建
創(chuàng)建Oracle臨時(shí)表,可以有兩種類(lèi)型的臨時(shí)表:會(huì)話級(jí)的臨時(shí)表和事務(wù)級(jí)的臨時(shí)表。
1)會(huì)話級(jí)的臨時(shí)表因?yàn)檫@這個(gè)臨時(shí)表中的數(shù)據(jù)和你的當(dāng)前會(huì)話有關(guān)系,當(dāng)你當(dāng)前SESSION不退出的情況下,臨時(shí)表中的數(shù)據(jù)就還存在,而當(dāng)你退出當(dāng)前 SESSION的時(shí)候,臨時(shí)表中的數(shù)據(jù)就全部沒(méi)有了,當(dāng)然這個(gè)時(shí)候你如果以另外一個(gè)SESSION登陸的時(shí)候是看不到另外一個(gè)SESSION中插入到臨時(shí)表中的數(shù)據(jù)的。
即兩個(gè)不同的SESSION所插入的數(shù)據(jù)是互不相干的。當(dāng)某一個(gè)SESSION退出之后臨時(shí)表中的數(shù)據(jù)就被截?cái)啵╰runcate table,即數(shù)據(jù)清空)了。會(huì)話級(jí)的臨時(shí)表創(chuàng)建方法:Create Global Temporary Table Table_Name(Col1 Type1,Col2 Type2……) On Commit Preserve Rows;
舉例create global temporary table Student(Stu_id Number(5),Class_id Number(5),Stu_Name Varchar2(8),Stu_Memo varchar2(200)) on Commit Preserve Rows ;
2)事務(wù)級(jí)臨時(shí)表是指該臨時(shí)表與事務(wù)相關(guān),當(dāng)進(jìn)行事務(wù)提交或者事務(wù)回滾的時(shí)候,臨時(shí)表中的數(shù)據(jù)將自行被截?cái)?,其他的?nèi)容和會(huì)話級(jí)的臨時(shí)表的一致(包括退出 SESSION的時(shí)候,事務(wù)級(jí)的臨時(shí)表也會(huì)被自動(dòng)截?cái)啵?/p>
事務(wù)級(jí)臨時(shí)表的創(chuàng)建方法:Create Global Temporary Table Table_Name(Col1 Type1,Col2 Type2……) On Commit Delete Rows;舉例:create global temporary table Classes(Class_id Number(5),Class_Name Varchar2(8),Class_Memo varchar2(200)) on Commit delete Rows ;
3)、兩種不通類(lèi)型的臨時(shí)表的區(qū)別:語(yǔ)法上,會(huì)話級(jí)Oracle 臨時(shí)表采用on commit preserve rows而事務(wù)級(jí)則采用on commit delete rows;用法上,會(huì)話級(jí)別只有當(dāng)會(huì)話結(jié)束臨時(shí)表中的數(shù)據(jù)才會(huì)被截?cái)啵沂聞?wù)級(jí)臨時(shí)表則不管是commit、rollback或者是會(huì)話結(jié)束,Oracle 臨時(shí)表中的數(shù)據(jù)都將被截?cái)唷?/p>
例子:
1)、會(huì)話級(jí)(Session關(guān)閉掉之后數(shù)據(jù)就沒(méi)有了,當(dāng)Commit的時(shí)候則數(shù)據(jù)還在,當(dāng)Rollback的時(shí)候則數(shù)據(jù)也是一樣被回滾):
復(fù)制代碼
- insert into student(stu_id,class_id,stu_name,stu_memo) values(1,1,''張三'',''福建'');
- insert into student(stu_id,class_id,stu_name,stu_memo) values(2,1,''劉德華'',''福州'');
- insert into student(stu_id,class_id,stu_name,stu_memo) values(3,2,''S.H.E'',''廈門(mén)'');
- SQL> select *from student ;
復(fù)制代碼STU_ID CLASS_ID STU_NAME STU_MEMO
------ -------- -------- ------------------------
1 1 張三 福建
2 1 劉德華 福州
3 2 S.H.E 廈門(mén)
4 2 張惠妹 廈門(mén)
復(fù)制代碼
- SQL> commit;
- Commit complete
- SQL> select * from student ;
復(fù)制代碼
- STU_ID CLASS_ID STU_NAME STU_MEMO
- ------ -------- -------- --------------------
1 1 張三 福建
2 1 劉德華 福州
3 2 S.H.E 廈門(mén)
4 2 張惠妹 廈門(mén)
復(fù)制代碼
- SQL>insert into student(stu_id,class_id,stu_name,stu_memo) values(4,2,''張惠妹'',''廈門(mén)'');
- 1 row inserted
- SQL> select * from student ;
上述的相關(guān)內(nèi)容就是對(duì)Oracle 臨時(shí)表的應(yīng)用的描述,希望會(huì)給你帶來(lái)一些幫助在此方面。
【編輯推薦】