程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> Oracle誤刪除表數據後的數據恢復詳解,oracle誤刪除

Oracle誤刪除表數據後的數據恢復詳解,oracle誤刪除

編輯:Oracle教程

Oracle誤刪除表數據後的數據恢復詳解,oracle誤刪除


Oracle誤刪除表數據後的恢復詳解
 
測試環境:
SYSTEM:IBM AIX 5L                         Oracle Version:10gR2
 

1. undo_retention參數的查詢與修改

使用show parameter undo命令查看當前的數據庫參數undo_retention設置。
顯示如下:
SQL> show parameter undo

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS2
undo_retention(保持力),900單位是秒,即15分鐘。
修改默認的undo_retention參數設置:
SQL> ALTER SYSTEM SET undo_retention=10800 SCOPE=BOTH;

System altered.

SQL> show parameter undo

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     10800
undo_tablespace                      string      UNDOTBS2
undo_retention 10800,單位秒,即3小時。

2. oracle誤刪除表數據後的的快速恢復功能方法

2.1 方法一
通過oracle提供的回閃功能

exec dbms_flashback.enable_at_time(to_date('2011-04-15 08:21:00','yyyy-mm-dd hh24:mi:ss'));
set serveroutput on
DECLARE r_temp hr.job_history%ROWTYPE;
CURSOR c_temp IS SELECT * FROM hr.job_history;
BEGIN
OPEN c_temp;
dbms_flashback.disable;
LOOP
FETCH c_temp INTO r_temp;
EXIT WHEN c_temp%NOTFOUND;
insert into hr.job_history(EMPLOYEE_ID,JOB_ID,START_DATE,END_DATE) values (r_temp.EMPLOYEE_ID,r_temp.JOB_ID,r_temp.START_DATE,r_temp.END_DATE);
commit;
END LOOP;
CLOSE c_temp;
END;

這種辦法可以將刪除的數據恢復到對應的表中,首先要保證該用戶有執行dbms_flashback包的權限。

2.2 方法二
insert into hr.job_history
select * from hr.job_history as of timestamp to_timestamp('2011-04-15 08:20:00', 'yyyy-mm-dd hh24:mi:ss');
這種方法簡單,容易掌握,功能和上面的一樣,此處的時間為你誤操作之前的時間,最好是離誤操作比較近的,因為oracle保存在回滾保持段裡的數據時間有一定的時間限制,這個限制由undo_retention 這個參數值決定。

查看FIRST_CHANGE#,NEXT_CHANGE#,FIRST_TIME
SQL> set pagesize 9999
SQL> col fscn for 999999999
SQL> col nscn for 999999999
SQL> select name,FIRST_CHANGE# fscn,NEXT_CHANGE# nscn,FIRST_TIME from v$archived_log;

當前的SCN為:
SQL> select dbms_flashback.get_system_change_number fscn from dual;
      FSCN
----------
   3435958

使用應用用戶嘗試閃回
SQL> connect username/password
Connected.

現有數據:
SQL> select count(*) from hs_passport;
  COUNT(*)
----------
    851998
創建恢復表:
SQL> create table hs_passport_recov as select * from hs_passport where 1=0;

Table created.

選擇SCN向前恢復:
SQL> select count(*) from hs_passport as of scn 12929970422;
  COUNT(*)
----------
    861686

嘗試多個SCN,獲取最佳值(如果能得知具體時間,那麼可以獲得准確的數據閃回)

SQL> select count(*) from hs_passport as of scn &scn;
Enter value for scn: 12929941968
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12929941968
 COUNT(*)
----------
  861684

SQL> /
Enter value for scn: 12927633776
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12927633776
select count(*) from hs_passport as of scn 12927633776
           *
ERROR at line 1:
ORA-01466: unable to read data - table definition has changed

SQL> /
Enter value for scn: 12929928784
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12929928784

 COUNT(*)
----------
  825110

SQL> /
Enter value for scn: 12928000000
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12928000000
select count(*) from hs_passport as of scn 12928000000
           *
ERROR at line 1:
ORA-01466: unable to read data - table definition has changed

最後選擇恢復到SCN為12929941968的時間點
SQL> insert into hs_passport_recov select * from hs_passport as of scn 12929941968;

861684 rows created.

SQL> commit;

Commit complete.

數據恢復簡單例子
在過去,如果用戶誤刪/更新了數據後,作為用戶並沒有什麼直接的方法來進行恢復,他們必須求助DBA來對數據庫進行恢復,到了Oracle9i,這一難堪的局面有所改善。Oracle 9i中提供了一項新的技術手段--閃回查詢,用戶使用閃回查詢可以及時取得誤操作前的數據,並可以針對錯誤進行相應的恢復措施,而這一切都無需DBA干預。

3. 下面我們通過一個例子來具體說明閃回查詢的用法

示例
3.1 使用閃回查詢前必須確定下面兩個參數:
UNDO_MANAGEMENT = AUTO
undo_retention = 10800;
這個時間可以隨便設,它表示在系統中保留提交了的UNDO信息的時間,10800就是保留3小時,即180分鐘。
3.2 使用閃回查詢

SQL> conn /as sysdba
Connected.
SQL> drop user lsf cascade;

User dropped.

SQL> create user lsf identified by lsf;

User created.

SQL> grant connect,resource to lsf;

Grant succeeded.

SQL> grant execute on dbms_flashback to lsf;

Grant succeeded.

SQL> conn lsf/lsf
Connected.
SQL> create table T(id int, name varchar2(20));

Table created.

SQL> insert into T values(1,'lsf');

1 row created.

SQL> insert into T values(2,'lsf');

1 row created.

SQL> insert into T values(3,'lsf');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from T;

    ID NAME
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf

SQL> set time on
10:12:50 SQL> delete from T where id=1;

1 row deleted.

10:13:02 SQL> commit;

Commit complete.

10:13:10 SQL> select * from T;

    ID NAME
---------- ------------------------------------------------------------
     2 lsf
     3 lsf

10:13:18 SQL> execute DBMS_FLASHBACK.ENABLE_AT_TIME(to_date('2011-04-15 10:12:50','YYYY-MM-DD HH24:MI:SS'));

PL/SQL procedure successfully completed.

10:13:50 SQL> select * from T;

    ID NAME
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf

10:13:57 SQL> execute DBMS_FLASHBACK.DISABLE;

PL/SQL procedure successfully completed.

10:15:48 SQL> select * from T;

    ID NAME
---------- ------------------------------------------------------------
     2 lsf
     3 lsf

3.3 使用閃回查詢恢復數據

10:16:59 SQL> truncate table T;

Table truncated.

10:18:15 SQL> select * from T;

no rows selected

10:18:22 SQL> insert into T values(1,'lsf');

1 row created.

10:19:42 SQL> insert into T values(2,'lsf');

1 row created.

10:19:48 SQL> insert into T values(3,'lsf');

1 row created.

10:19:55 SQL> insert into T values(4,'lsf');

1 row created.

10:20:07 SQL> insert into T values(5,'lsf');

1 row created.

10:20:15 SQL> insert into T values(6,'lsf');

1 row created.

10:20:21 SQL> commit;

Commit complete.

10:20:26 SQL> select * from T;

    ID NAME
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf
     4 lsf
     5 lsf
     6 lsf

6 rows selected.

10:20:56 SQL> delete T;

6 rows deleted.

10:21:27 SQL> commit;

Commit complete.

10:21:40 SQL> declare
10:22:29  2 cursor flash_recover is
10:22:43  3 select * from T;
10:22:50  4 t_recode T%rowtype;
10:23:11  5 begin
10:23:14  6 DBMS_FLASHBACK.ENABLE_AT_TIME(to_date('2011-04-15 10:20:56','YYYY-MM-DD HH24:MI:SS'));
10:24:22  7 open flash_recover;
10:24:39  8 DBMS_FLASHBACK.DISABLE;
10:24:59  9 loop
10:25:05 10 FETCH flash_recover into t_recode;
10:25:24 11 EXIT WHEN flash_recover%NOTFOUND;
10:25:45 12 insert into T values(t_recode.id,t_recode.name);
10:26:35 13 end loop;
10:26:39 14 CLOSE FLASH_RECOVER;
10:26:50 15 commit;
10:26:56 16 end;
10:26:58 17 /

PL/SQL procedure successfully completed.

10:27:00 SQL> select * from T;

    ID NAME
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf
     4 lsf
     5 lsf
     6 lsf

6 rows selected.

我們可以已經恢復了所有的6條紀錄,但是由於閃回查詢的局限性,有可能不能恢復所有的6條記錄,原因就在下面。

4. 局限性
4.1 閃回查詢是基於SCN的,雖然我們執行的是:
DBMS_FLASHBACK.ENABLE_AT_TIME(to_date('2011-04-15 10:20:56','YYYY-MM-DD HH24:MI:SS'));
但Oracle並不會精確的這個時間點,而是ROUND DOWN到最近的一次SCN,然後從這個SCN開始進行恢復。而Oracle 9i是每五分鐘記錄一次SCN的,並將SCN和對應時間的映射做個紀錄。
因此如果使用DBMS_FLASHBACK.ENABLE_AT_TIME來進行恢復,為了避免恢復失敗,我們可以先等5分鐘,然後再進行恢復。
使用DBMS_FLASHBACK.ENABLE_AT_TIME進行恢復還有一個缺點,那就是在Oracle 9i中SCN和對應時間的映射信息只會保留5天,因此我們無法通過DBMS_FLASHBACK.ENABLE_AT_TIME來恢復5天前的數據。如果你想使用閃回查詢來恢復5天前的數據,你必須自己來確定需要恢復的SCN,然後使用DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(SCN_NUMBER); 來定位你的恢復時間點,下面是使用方法:

10:27:27 SQL> VARIABLE SCN_SAVE NUMBER;
10:32:47 SQL> EXECUTE :SCN_SAVE := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER;

PL/SQL procedure successfully completed.

10:33:24 SQL> print SCN_SAVE;

 SCN_SAVE
----------
  3438420

10:33:41 SQL> execute DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(:SCN_SAVE);

PL/SQL procedure successfully completed.

10:34:31 SQL> select * from T;

    ID NAME
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf
     4 lsf
     5 lsf
     6 lsf

6 rows selected.

另外,在使用DBMS_FLASHBACK.ENABLE_AT_TIME前,你必須設定你的NLS_DATE_FORMAT的精確程度,Oracle默認的是精確到天,如果你不設定,像上面的例子你不會得到預期結果。
4.2 如果你使用sysdate和DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER來獲取時間點或者SCN值,你必須注意它們取得都是當前的時間點和SCN值。
4.3 你只能在事務開始時進入閃回查詢模式,如果之前有DML操作,則必須COMMIT。
4.4 閃回查詢無法恢復到表結構改變之前,因為閃回查詢使用的當前的數據字典。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved