主頁(yè) > 知識(shí)庫(kù) > Oracle誤刪除表數(shù)據(jù)后的數(shù)據(jù)恢復(fù)詳解

Oracle誤刪除表數(shù)據(jù)后的數(shù)據(jù)恢復(fù)詳解

熱門(mén)標(biāo)簽:廣東營(yíng)銷(xiāo)智能外呼系統(tǒng)商家 地圖標(biāo)注怎么保存 車(chē)瑪仕極限運(yùn)動(dòng)場(chǎng)所地圖標(biāo)注 外呼電話系統(tǒng)用卡嗎 七日殺a19.5全地圖標(biāo)注 高德地圖標(biāo)注公司名字大全 騰訊地圖標(biāo)注要費(fèi)用嗎 電渠外呼系統(tǒng) N個(gè)你智能電銷(xiāo)機(jī)器人

Oracle誤刪除表數(shù)據(jù)后的恢復(fù)詳解
 
測(cè)試環(huán)境:
SYSTEM:IBM AIX 5L                         Oracle Version:10gR2
 

1. undo_retention參數(shù)的查詢與修改

使用show parameter undo命令查看當(dāng)前的數(shù)據(jù)庫(kù)參數(shù)undo_retention設(shè)置。
顯示如下:
SQL> show parameter undo

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS2
undo_retention(保持力),900單位是秒,即15分鐘。
修改默認(rèn)的undo_retention參數(shù)設(shè)置:
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小時(shí)。

2. oracle誤刪除表數(shù)據(jù)后的的快速恢復(fù)功能方法

2.1 方法一
通過(guò)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;

這種辦法可以將刪除的數(shù)據(jù)恢復(fù)到對(duì)應(yīng)的表中,首先要保證該用戶有執(zhí)行dbms_flashback包的權(quán)限。

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');
這種方法簡(jiǎn)單,容易掌握,功能和上面的一樣,此處的時(shí)間為你誤操作之前的時(shí)間,最好是離誤操作比較近的,因?yàn)閛racle保存在回滾保持段里的數(shù)據(jù)時(shí)間有一定的時(shí)間限制,這個(gè)限制由undo_retention 這個(gè)參數(shù)值決定。

查看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;

當(dāng)前的SCN為:
SQL> select dbms_flashback.get_system_change_number fscn from dual;
      FSCN
----------
   3435958

使用應(yīng)用用戶嘗試閃回
SQL> connect username/password
Connected.

現(xiàn)有數(shù)據(jù):
SQL> select count(*) from hs_passport;
  COUNT(*)
----------
    851998
創(chuàng)建恢復(fù)表:
SQL> create table hs_passport_recov as select * from hs_passport where 1=0;

Table created.

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

嘗試多個(gè)SCN,獲取最佳值(如果能得知具體時(shí)間,那么可以獲得準(zhǔn)確的數(shù)據(jù)閃回)

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

最后選擇恢復(fù)到SCN為12929941968的時(shí)間點(diǎn)
SQL> insert into hs_passport_recov select * from hs_passport as of scn 12929941968;

861684 rows created.

SQL> commit;

Commit complete.

數(shù)據(jù)恢復(fù)簡(jiǎn)單例子
在過(guò)去,如果用戶誤刪/更新了數(shù)據(jù)后,作為用戶并沒(méi)有什么直接的方法來(lái)進(jìn)行恢復(fù),他們必須求助DBA來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行恢復(fù),到了Oracle9i,這一難堪的局面有所改善。Oracle 9i中提供了一項(xiàng)新的技術(shù)手段--閃回查詢,用戶使用閃回查詢可以及時(shí)取得誤操作前的數(shù)據(jù),并可以針對(duì)錯(cuò)誤進(jìn)行相應(yīng)的恢復(fù)措施,而這一切都無(wú)需DBA干預(yù)。

3. 下面我們通過(guò)一個(gè)例子來(lái)具體說(shuō)明閃回查詢的用法

示例
3.1 使用閃回查詢前必須確定下面兩個(gè)參數(shù):
UNDO_MANAGEMENT = AUTO
undo_retention = 10800;
這個(gè)時(shí)間可以隨便設(shè),它表示在系統(tǒng)中保留提交了的UNDO信息的時(shí)間,10800就是保留3小時(shí),即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 使用閃回查詢恢復(fù)數(shù)據(jù)

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.

我們可以已經(jīng)恢復(fù)了所有的6條紀(jì)錄,但是由于閃回查詢的局限性,有可能不能恢復(fù)所有的6條記錄,原因就在下面。

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

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前,你必須設(shè)定你的NLS_DATE_FORMAT的精確程度,Oracle默認(rèn)的是精確到天,如果你不設(shè)定,像上面的例子你不會(huì)得到預(yù)期結(jié)果。
4.2 如果你使用sysdate和DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER來(lái)獲取時(shí)間點(diǎn)或者SCN值,你必須注意它們?nèi)〉枚际钱?dāng)前的時(shí)間點(diǎn)和SCN值。
4.3 你只能在事務(wù)開(kāi)始時(shí)進(jìn)入閃回查詢模式,如果之前有DML操作,則必須COMMIT。
4.4 閃回查詢無(wú)法恢復(fù)到表結(jié)構(gòu)改變之前,因?yàn)殚W回查詢使用的當(dāng)前的數(shù)據(jù)字典。

您可能感興趣的文章:
  • oracle數(shù)據(jù)庫(kù)的刪除方法詳解
  • Oracle刪除數(shù)據(jù)報(bào)ORA 02292錯(cuò)誤的巧妙解決方法
  • oracle 數(shù)據(jù)按主鍵刪除慢問(wèn)題的解決方法
  • Oracle刪除重復(fù)的數(shù)據(jù),Oracle數(shù)據(jù)去重復(fù)
  • Oracle數(shù)據(jù)庫(kù)中的級(jí)聯(lián)查詢、級(jí)聯(lián)刪除、級(jí)聯(lián)更新操作教程
  • 徹底刪除Oracle數(shù)據(jù)庫(kù)的方法
  • oracle查詢重復(fù)數(shù)據(jù)和刪除重復(fù)記錄示例分享
  • oracle數(shù)據(jù)庫(kù)添加或刪除一列的sql語(yǔ)句
  • oracle 批量刪除表數(shù)據(jù)的幾種方法

標(biāo)簽:棗莊 長(zhǎng)沙 來(lái)賓 遼寧 玉樹(shù) 贛州 蘇州 大興安嶺

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle誤刪除表數(shù)據(jù)后的數(shù)據(jù)恢復(fù)詳解》,本文關(guān)鍵詞  Oracle,誤,刪除,表,數(shù)據(jù),后的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Oracle誤刪除表數(shù)據(jù)后的數(shù)據(jù)恢復(fù)詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Oracle誤刪除表數(shù)據(jù)后的數(shù)據(jù)恢復(fù)詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章