ºìÁªLinuxÃÅ»§
Linux°ïÖú

OracleÁÙʱ±í ÓÅ»¯²éѯËÙ¶È

·¢²¼Ê±¼ä:2006-10-17 23:03:57À´Ô´:ºìÁª×÷Õß:linuxcms
1¡¢Ç°ÑÔ
Ä¿Ç°ËùÓÐʹÓÃOracle×÷ΪÊý¾Ý¿âÖ§³Åƽ̨µÄÓ¦Ó㬴󲿷ÖÊý¾ÝÁ¿±È½ÏÅÓ´óµÄϵͳ£¬¼´±íµÄÊý¾ÝÁ¿Ò»°ãÇé¿ö϶¼ÊÇÔÚ°ÙÍò¼¶ÒÔÉϵÄÊý¾ÝÁ¿¡£µ±È»ÔÚOracleÖд´½¨·ÖÇøÊÇÒ»ÖÖ²»´íµÄÑ¡Ôñ£¬µ«Êǵ±Äã·¢ÏÖÄãµÄÓ¦ÓÃÓжàÕűí¹ØÁªµÄʱºò£¬²¢ÇÒÕâЩ±í´ó²¿·Ö¶¼ÊDZȽÏÅӴ󣬶øÄã¹ØÁªµÄʱºò·¢ÏÖÆäÖеÄijһÕÅ»òÕßij¼¸Õűí¹ØÁªÖ®ºóµÃµ½µÄ½á¹û¼¯·Ç³£Ð¡²¢ÇÒ²éѯµÃµ½Õâ¸ö½á¹û¼¯µÄËٶȷdz£¿ì£¬ÄÇôÕâ¸öʱºòÎÒ¿¼ÂÇÔÚOracleÖд´½¨¡°ÁÙʱ±í¡±¡£
ÎÒ¶ÔÁÙʱ±íµÄÀí½â£ºÔÚOracleÖд´½¨Ò»ÕÅ±í£¬Õâ¸ö±í²»ÓÃÓÚÆäËûµÄʲô¹¦ÄÜ£¬Ö÷ÒªÓÃÓÚ×Ô¼ºµÄÈí¼þϵͳһЩÌØÓй¦ÄܲÅÓõģ¬¶øµ±ÄãÓÃÍêÖ®ºó±íÖеÄÊý¾Ý¾ÍûÓÃÁË¡£OracleµÄÁÙʱ±í´´½¨Ö®ºó»ù±¾²»Õ¼Óñí¿Õ¼ä£¬Èç¹ûÄãûÓÐÖ¸¶¨ÁÙʱ±í£¨°üÀ¨ÁÙʱ±íµÄË÷Òý£©´æ·ÅµÄ±í¿ÕµÄʱºò£¬Äã²åÈëµ½ÁÙʱ±íµÄÊý¾ÝÊÇ´æ·ÅÔÚORACLEϵͳµÄÁÙʱ±í¿Õ¼äÖУ¨TEMP£©¡£

2¡¢ÁÙʱ±íµÄ´´½¨
´´½¨OracleÁÙʱ±í£¬¿ÉÒÔÓÐÁ½ÖÖÀàÐ͵ÄÁÙʱ±í£º»á»°¼¶µÄÁÙʱ±íºÍÊÂÎñ¼¶µÄÁÙʱ±í¡£
1£©»á»°¼¶µÄÁÙʱ±íÒòΪÕâÕâ¸öÁÙʱ±íÖеÄÊý¾ÝºÍÄãµÄµ±Ç°»á»°ÓйØϵ£¬µ±Ä㵱ǰSESSION²»Í˳öµÄÇé¿öÏ£¬ÁÙʱ±íÖеÄÊý¾Ý¾Í»¹´æÔÚ£¬¶øµ±ÄãÍ˳öµ±Ç°SESSIONµÄʱºò£¬ÁÙʱ±íÖеÄÊý¾Ý¾ÍÈ«²¿Ã»ÓÐÁË£¬µ±È»Õâ¸öʱºòÄãÈç¹ûÒÔÁíÍâÒ»¸öSESSIONµÇ½µÄʱºòÊÇ¿´²»µ½ÁíÍâÒ»¸öSESSIONÖвåÈëµ½ÁÙʱ±íÖеÄÊý¾ÝµÄ¡£¼´Á½¸ö²»Í¬µÄSESSIONËù²åÈëµÄÊý¾ÝÊÇ»¥²»Ïà¸ÉµÄ¡£µ±Ä³Ò»¸öSESSIONÍ˳öÖ®ºóÁÙʱ±íÖеÄÊý¾Ý¾Í±»½Ø¶Ï£¨truncate table£¬¼´Êý¾ÝÇå¿Õ£©ÁË¡£»á»°¼¶µÄÁÙʱ±í´´½¨·½·¨£º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£©ÊÂÎñ¼¶ÁÙʱ±íÊÇÖ¸¸ÃÁÙʱ±íÓëÊÂÎñÏà¹Ø£¬µ±½øÐÐÊÂÎñÌá½»»òÕßÊÂÎñ»Ø¹öµÄʱºò£¬ÁÙʱ±íÖеÄÊý¾Ý½«×ÔÐб»½Ø¶Ï£¬ÆäËûµÄÄÚÈݺͻỰ¼¶µÄÁÙʱ±íµÄÒ»Ö£¨°üÀ¨Í˳öSESSIONµÄʱºò£¬ÊÂÎñ¼¶µÄÁÙʱ±íÒ²»á±»×Ô¶¯½Ø¶Ï£©¡£ÊÂÎñ¼¶ÁÙʱ±íµÄ´´½¨·½·¨£º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£©¡¢Á½ÖÖ²»Í¨ÀàÐ͵ÄÁÙʱ±íµÄÇø±ð£ºÓï·¨ÉÏ£¬»á»°¼¶ÁÙʱ±í²ÉÓÃon commit preserve rows¶øÊÂÎñ¼¶Ôò²ÉÓÃon commit delete rows£»Ó÷¨ÉÏ£¬»á»°¼¶±ðÖ»Óе±»á»°½áÊøÁÙʱ±íÖеÄÊý¾Ý²Å»á±»½Ø¶Ï£¬¶øÇÒÊÂÎñ¼¶ÁÙʱ±íÔò²»¹ÜÊÇcommit¡¢rollback»òÕßÊǻỰ½áÊø£¬ÁÙʱ±íÖеÄÊý¾Ý¶¼½«±»½Ø¶Ï¡£
3¡¢Àý×Ó£º
1£©¡¢»á»°¼¶£¨Session¹Ø±ÕµôÖ®ºóÊý¾Ý¾ÍûÓÐÁË£¬µ±CommitµÄʱºòÔòÊý¾Ý»¹ÔÚ£¬µ±RollbackµÄʱºòÔòÊý¾ÝÒ²ÊÇÒ»Ñù±»»Ø¹ö£©£º
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','ÏÃÃÅ');
SQL> select *from student ;

STU_ID CLASS_ID STU_NAME STU_MEMO
------ -------- -------- --------------------------------------------------------------------------------
1 1 ÕÅÈý ¸£½¨
2 1 ÁõµÂ»ª ¸£ÖÝ
3 2 S.H.E ÏÃÃÅ
4 2 ÕÅ»ÝÃà ÏÃÃÅ

SQL> commit;

Commit complete

SQL> select * from student ;

STU_ID CLASS_ID STU_NAME STU_MEMO
------ -------- -------- --------------------------------------------------------------------------------
1 1 ÕÅÈý ¸£½¨
2 1 ÁõµÂ»ª ¸£ÖÝ
3 2 S.H.E ÏÃÃÅ
4 2 ÕÅ»ÝÃà ÏÃÃÅ

SQL>insert into student(stu_id,class_id,stu_name,stu_memo) values(4,2,'ÕÅ»ÝÃÃ','ÏÃÃÅ');

1 row inserted

SQL> select * from student ;

STU_ID CLASS_ID STU_NAME STU_MEMO
------ -------- -------- --------------------------------------------------------------------------------
1 1 ÕÅÈý ¸£½¨
2 1 ÁõµÂ»ª ¸£ÖÝ
3 2 S.H.E ÏÃÃÅ
4 2 ÕÅ»ÝÃà ÏÃÃÅ
4 2 ÕÅ»ÝÃà ÏÃÃÅ

SQL> rollback ;

Rollback complete

SQL> select * from student ;

STU_ID CLASS_ID STU_NAME STU_MEMO
------ -------- -------- --------------------------------------------------------------------------------
1 1 ÕÅÈý ¸£½¨
2 1 ÁõµÂ»ª ¸£ÖÝ
3 2 S.H.E ÏÃÃÅ
4 2 ÕÅ»ÝÃà ÏÃÃÅ

SQL>
2£©¡¢ÊÂÎñ¼¶£¨CommitÖ®ºó¾Íɾ³ýÊý¾Ý£©£º±¾Àý×Ó½«²ÉÓÃÒÔϵÄÊý¾Ý£º
insert into classes(Class_id,Class_Name,Class_Memo) values(1,'¼ÆËã»ú','9608');
insert into classes(Class_id,Class_Name,Class_Memo) values(2,'¾­¼ÃÐÅÏ¢','9602');
insert into classes(Class_id,Class_Name,Class_Memo) values(3,'¾­¼ÃÐÅÏ¢','9603');
ÔÚÒ»¸öSESSIONÖУ¨±ÈÈçSQLPLUSµÇ½£©²åÈëÉÏÃæ3Ìõ¼Ç¼£¬È»ºóÔÙÒÔÁíÍâÒ»¸öSESSION£¨ÓÃSQLPLUSÔٵǽһ´Î£©µÇ½£¬µ±Äãselect * from classes;µÄʱºò£¬classes±íÊǿյģ¬¶øÄãÔÙµÚÒ»´ÎµÇ½µÄSQLPLUSÖÐselectµÄʱºò¿ÉÒÔ²éѯµ½£¬Õâ¸öʱºòÄãûÓнøÐÐcommit»òÕßrollback֮ǰÄã¿ÉÒԶԸղŲåÈëµÄ3Ìõ¼Ç¼½øÐÐupdate¡¢deleteµÈ²Ù×÷£¬µ±Äã½øÐÐcommit»òÕßrollbackµÄʱºò£¬Õâ¸öʱºòÓÉÓÚÄãµÄ±íÊÇÊÂÎñ¼¶µÄÁÙʱ±í£¬ÄÇôÔÚ²åÈëÊý¾ÝµÄsessionÒ²¿´²»µ½Êý¾ÝÁË£¬Õâ¸öʱºòÊý¾Ý¾ÍÒѾ­±»½Ø¶ÏÁË¡£
ÔËÐнá¹ûÈçÏ£º
SQL> insert into classes(Class_id,Class_Name,Class_Memo) values(1,'¼ÆËã»ú','9608');

1 row inserted

SQL> insert into classes(Class_id,Class_Name,Class_Memo) values(2,'¾­¼ÃÐÅÏ¢','9602');

1 row inserted

SQL> insert into classes(Class_id,Class_Name,Class_Memo) values(3,'¾­¼ÃÐÅÏ¢','9603');

1 row inserted

SQL> update classes set class_memo ='' where class_id=3 ;

1 row updated

SQL> select * from classes ;

CLASS_ID CLASS_NAME CLASS_MEMO
-------- ---------- --------------------------------------------------------------------------------
1 ¼ÆËã»ú 9608
2 ¾­¼ÃÐÅÏ¢ 9602
3 ¾­¼ÃÐÅÏ¢

SQL> delete from classes where class_id=3 ;

1 row deleted

SQL> select * from classes ;

CLASS_ID CLASS_NAME CLASS_MEMO
-------- ---------- --------------------------------------------------------------------------------
1 ¼ÆËã»ú 9608
2 ¾­¼ÃÐÅÏ¢ 9602
SQL> commit;

Commit complete

SQL> select *from classes ;

CLASS_ID CLASS_NAME CLASS_MEMO
-------- ---------- --------------------------------------------------------------------------------

SQL>
ÔÙÖظ´²åÈëÒ»´Î£¬È»ºórollback¡£
SQL> Rollback ;

Rollback complete

SQL> select * from classes ;

CLASS_ID CLASS_NAME CLASS_MEMO
-------- ---------- --------------------------------------------------------------------------------

SQL>

4¡¢ÁÙʱ±íµÄÓ¦ÓÃ
1£©¡¢µ±Ä³Ò»¸öSQLÓï¾ä¹ØÁªµÄ±íÔÚ2Õż°ÒÔÉÏ£¬²¢ÇÒºÍһЩС±í¹ØÁª¡£¿ÉÒÔ²ÉÓý«´ó±í½øÐзֲð²¢Çҵõ½±È½ÏСµÄ½á¹û¼¯ºÏ´æ·ÅÔÚÁÙʱ±íÖС£
2£©¡¢³ÌÐòÖ´Ðйý³ÌÖпÉÄÜÐèÒª´æ·ÅһЩÁÙʱµÄÊý¾Ý£¬ÕâЩÊý¾ÝÔÚÕû¸ö³ÌÐòµÄ»á»°¹ý³ÌÖж¼ÐèÒªÓõĵȵȡ£
5¡¢×¢ÒâÊÂÏ
1£©¡¢ÁÙʱ±íµÄË÷ÒýÒÔ¼°¶Ô±íµÄÐ޸ġ¢É¾³ýµÈºÍÕý³£µÄ±íÊÇÒ»Öµġ£
2£©¡¢OracleµÄÁÙʱ±íÊÇOracle8i²ÅÖ§³ÖµÄ¹¦ÄÜÌØÐÔ£¬Èç¹ûÄãµÄOracle°æ±¾±È½ÏµÍµÄ»°£¬ÄÇô¾Í¿ÉÄÜûÓа취Óõ½ÁË£¬Èç¹ûÄãµÄOracle°æ±¾ÊÇ8iµÄ»°£¬Ä㻹ÐèÒª°Ñ$ORACLE_HOME/admin/${ORACLE_SID}/pfileĿ¼ÏµÄinit.ora³õʼ²ÎÊýÅäÖÃÎļþµÄcompatibleÐÞ¸ÄΪcompatible = "8.1.0"£¬ÎҵķþÎñÆ÷ÉϾÍÊÇÕâÑù×ÓÅäÖõġ£µ±È»Ò²¿ÉÒÔÐÞ¸ÄΪcompatible = "8.1.6"

ÒÔÉÏÊÇÎÒÔÚ¶Ô´ó±í½øÐÐÓÅ»¯µÄʱºò²ÉÓõÄһЩÊֶΣ¬Ð§¹ûÏÔÖø¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ