閃回 錯誤的DML 操作

楊奇龍發表於2010-04-18

在oracle10g版本之前,如果使用者對一個表進行了錯誤的DML操作並且提交了這些操作,而且之前使用者也沒備份,要恢復該表到操作之前的狀態,就要對這個表進行不完全回覆。oracle10g中只要使用一條命令就可以了!oracle是利用還原段中的資料來執行這一恢復的。

提交DML操作之後,該操作所使用的還原段就可以被其他的操作使用了,為了保證在進行閃回操作時這些資料仍然在還原段中,可能要重新設定undoretention的數值,單位為妙,表示事務提交的資料至少在還原段裡停留的時間。

實驗如下:

SQL> create table test as select * from  emp;--首先建立一個test表用於實驗。

表已建立。

SQL> select *  from test;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7369 SMITH      CLERK           7902 17-12月-80            800           
        20                                                                     
                                                                               
      7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300
        30                                                                     
                                                                               
      7521 WARD       SALESMAN        7698 22-2月 -81           1250        500
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7566 JONES      MANAGER         7839 02-4月 -81           2975           
        20                                                                     
                                                                               
      7654 MARTIN     SALESMAN        7698 28-9月 -81           1250       1400
        30                                                                     
                                                                               
      7698 BLAKE      MANAGER         7839 01-5月 -81           2850           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7782 CLARK      MANAGER         7839 09-6月 -81           2450           
        10                                                                     
                                                                               
      7788 SCOTT      ANALYST         7566 19-4月 -87           3000           
        20                                                                     
                                                                               
      7839 KING       PRESIDENT            17-11月-81           5000           
        10                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7844 TURNER     SALESMAN        7698 08-9月 -81           1500          0
        30                                                                     
                                                                               
      7876 ADAMS      CLERK           7788 23-5月 -87           1100           
        20                                                                     
                                                                               
      7900 JAMES      CLERK           7698 03-12月-81            950           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7902 FORD       ANALYST         7566 03-12月-81           3000           
        20                                                                     
                                                                               
      7934 MILLER     CLERK           7782 23-1月 -82           1300           
        10                                                                     
                                                                               

已選擇14行。

SQL> conn system/yang --
已連線。

2  更改undo_retention的值。
SQL> show parameter undo_retention

NAME                                 TYPE        VALUE                         
------------------------------------ ----------- ------------------------------
undo_retention                       integer     900                           
SQL> alter system set undo_retention =7200 ;

系統已更改。

SQL> show parameter undo_retention

NAME                                 TYPE        VALUE                         
------------------------------------ ----------- ------------------------------
undo_retention                       integer     7200         

3 對test表進行DML操作                 

SQL> update test set sal =9999;

已更新14行。

4。接下來,使用帶有version子句的查詢語句獲取剛剛所做的DML操作的version_ID 和其他相關資訊。versions_xid,-- 事務號,minvalue 最小值, maxvalue 最大值

SQL> select versions_xid,empno,ename,sal
  2  from test
  3  versions between scn minvalue and maxvalue
  4  where empno =7900;

VERSIONS_XID          EMPNO ENAME             SAL                              
---------------- ---------- ---------- ----------                              
                       7900 JAMES             950        

由於還沒有提交DML操作,所以versionid 為空。進行提交。                      

SQL> commit;

提交完成。

SQL> select versions_xid,empno,ename,sal
  2  from test
  3  versions between scn minvalue and maxvalue
  4  where empno =7900;

VERSIONS_XID          EMPNO ENAME             SAL                              
---------------- ---------- ---------- ----------                              
0800050012030000       7900 JAMES            9999                              
                       7900 JAMES             950         

此時 versionid 已經 有值了,為了使用閃回功能,我們要進入到system使用者下。                     

SQL> conn system/yang
已連線。
SQL> col operation for a10
SQL> col undo_sql for a80
SQL> select operation,undo_sql
  2  from flashback_transaction_query
  3  where xid = hextoraw('0800050012030000');

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1300' where ROWID = 'AAARWCAAEAAAAGMAAN';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '3000' where ROWID = 'AAARWCAAEAAAAGMAAM';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '950' where ROWID = 'AAARWCAAEAAAAGMAAL';    
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1100' where ROWID = 'AAARWCAAEAAAAGMAAK';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1500' where ROWID = 'AAARWCAAEAAAAGMAAJ';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '5000' where ROWID = 'AAARWCAAEAAAAGMAAI';   
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '3000' where ROWID = 'AAARWCAAEAAAAGMAAH';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1250' where ROWID = 'AAARWCAAEAAAAGMAAC';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '2975' where ROWID = 'AAARWCAAEAAAAGMAAD';   
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1250' where ROWID = 'AAARWCAAEAAAAGMAAE';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '2850' where ROWID = 'AAARWCAAEAAAAGMAAF';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '2450' where ROWID = 'AAARWCAAEAAAAGMAAG';   
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1600' where ROWID = 'AAARWCAAEAAAAGMAAB';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '800' where ROWID = 'AAARWCAAEAAAAGMAAA';    
                                                                               
BEGIN                                                                          
                                                                               
                                                                               

已選擇15行。

查詢test表的內容,看見所有的工資都變為了9999

SQL> select * from scott.test ;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7369 SMITH      CLERK           7902 17-12月-80           9999           
        20                                                                     
                                                                               
      7499 ALLEN      SALESMAN        7698 20-2月 -81           9999        300
        30                                                                     
                                                                               
      7521 WARD       SALESMAN        7698 22-2月 -81           9999        500
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7566 JONES      MANAGER         7839 02-4月 -81           9999           
        20                                                                     
                                                                               
      7654 MARTIN     SALESMAN        7698 28-9月 -81           9999       1400
        30                                                                     
                                                                               
      7698 BLAKE      MANAGER         7839 01-5月 -81           9999           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7782 CLARK      MANAGER         7839 09-6月 -81           9999           
        10                                                                     
                                                                               
      7788 SCOTT      ANALYST         7566 19-4月 -87           9999           
        20                                                                     
                                                                               
      7839 KING       PRESIDENT            17-11月-81           9999           
        10                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7844 TURNER     SALESMAN        7698 08-9月 -81           9999          0
        30                                                                     
                                                                               
      7876 ADAMS      CLERK           7788 23-5月 -87           9999           
        20                                                                     
                                                                               
      7900 JAMES      CLERK           7698 03-12月-81           9999           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7902 FORD       ANALYST         7566 03-12月-81           9999           
        20                                                                     
                                                                               
      7934 MILLER     CLERK           7782 23-1月 -82           9999           
        10                                                                     
                                                                               

已選擇14行。

6 如果要對剛才的操作進行恢復,則先要使用下面的查詢獲得DML事務所對應的SCN號

SQL> select operation,START_SCN
  2  from flashback_transaction_query
  3  where xid=hextoraw('0800050012030000');

OPERATION   START_SCN                                                          
---------- ----------                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          

OPERATION   START_SCN                                                          
---------- ----------                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
BEGIN         1371887                                                          

已選擇15行。

---------------這個是當前的SCN號-------------

SQL> select current_scn from v$database;

CURRENT_SCN                                                                    
-----------                                                                    
    1372969                                                                    

SQL> flashback table scott.test to SCN 1371887;
flashback table scott.test to SCN 1371887
                      *
第 1 行出現錯誤:
ORA-08189: 因為未啟用行移動功能, 不能閃回表

出現此錯誤是因為在閃回一個表之前,要先將該表的行移動功能開啟,oracle預設是關閉的。
SQL> alter table scott.test enable row movement;

表已更改。

執行閃回操作。

SQL> flashback table scott.test to SCN 1371887;

閃回完成。

SQL> select * from scott.test;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7369 SMITH      CLERK           7902 17-12月-80            800           
        20                                                                     
                                                                               
      7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300
        30                                                                     
                                                                               
      7521 WARD       SALESMAN        7698 22-2月 -81           1250        500
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7566 JONES      MANAGER         7839 02-4月 -81           2975           
        20                                                                     
                                                                               
      7654 MARTIN     SALESMAN        7698 28-9月 -81           1250       1400
        30                                                                     
                                                                               
      7698 BLAKE      MANAGER         7839 01-5月 -81           2850           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7782 CLARK      MANAGER         7839 09-6月 -81           2450           
        10                                                                     
                                                                               
      7788 SCOTT      ANALYST         7566 19-4月 -87           3000           
        20                                                                     
                                                                               
      7839 KING       PRESIDENT            17-11月-81           5000           
        10                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7844 TURNER     SALESMAN        7698 08-9月 -81           1500          0
        30                                                                     
                                                                               
      7876 ADAMS      CLERK           7788 23-5月 -87           1100           
        20                                                                     
                                                                               
      7900 JAMES      CLERK           7698 03-12月-81            950           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7902 FORD       ANALYST         7566 03-12月-81           3000           
        20                                                                     
                                                                               
      7934 MILLER     CLERK           7782 23-1月 -82           1300           
        10                                                                     
                                                                               

已選擇14行。

成功。。。。。。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-660067/,如需轉載,請註明出處,否則將追究法律責任。

相關文章