Oracle誤刪除表數(shù)據(jù)后的恢復(fù)詳解
測試環(huán)境:
SYSTEM:IBM AIX 5L Oracle Version:10gR2
1. undo_retention參數(shù)的查詢與修改
使用show parameter undo命令查看當前的數(shù)據(jù)庫參數(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分鐘。
修改默認的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小時。
2. oracle誤刪除表數(shù)據(jù)后的的快速恢復(fù)功能方法
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;
這種辦法可以將刪除的數(shù)據(jù)恢復(fù)到對應(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');
這種方法簡單,容易掌握,功能和上面的一樣,此處的時間為你誤操作之前的時間,最好是離誤操作比較近的,因為oracle保存在回滾保持段里的數(shù)據(jù)時間有一定的時間限制,這個限制由undo_retention 這個參數(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;
當前的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
嘗試多個SCN,獲取最佳值(如果能得知具體時間,那么可以獲得準確的數(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的時間點
SQL> insert into hs_passport_recov select * from hs_passport as of scn 12929941968;
861684 rows created.
SQL> commit;
Commit complete.
數(shù)據(jù)恢復(fù)簡單例子
在過去,如果用戶誤刪/更新了數(shù)據(jù)后,作為用戶并沒有什么直接的方法來進行恢復(fù),他們必須求助DBA來對數(shù)據(jù)庫進行恢復(fù),到了Oracle9i,這一難堪的局面有所改善。Oracle 9i中提供了一項新的技術(shù)手段--閃回查詢,用戶使用閃回查詢可以及時取得誤操作前的數(shù)據(jù),并可以針對錯誤進行相應(yīng)的恢復(fù)措施,而這一切都無需DBA干預(yù)。
3. 下面我們通過一個例子來具體說明閃回查詢的用法
示例
3.1 使用閃回查詢前必須確定下面兩個參數(shù):
UNDO_MANAGEMENT = AUTO
undo_retention = 10800;
這個時間可以隨便設(shè),它表示在系統(tǒng)中保留提交了的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 使用閃回查詢恢復(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條紀錄,但是由于閃回查詢的局限性,有可能不能恢復(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并不會精確的這個時間點,而是ROUND DOWN到最近的一次SCN,然后從這個SCN開始進行恢復(fù)。而Oracle 9i是每五分鐘記錄一次SCN的,并將SCN和對應(yīng)時間的映射做個紀錄。
因此如果使用DBMS_FLASHBACK.ENABLE_AT_TIME來進行恢復(fù),為了避免恢復(fù)失敗,我們可以先等5分鐘,然后再進行恢復(fù)。
使用DBMS_FLASHBACK.ENABLE_AT_TIME進行恢復(fù)還有一個缺點,那就是在Oracle 9i中SCN和對應(yīng)時間的映射信息只會保留5天,因此我們無法通過DBMS_FLASHBACK.ENABLE_AT_TIME來恢復(fù)5天前的數(shù)據(jù)。如果你想使用閃回查詢來恢復(fù)5天前的數(shù)據(jù),你必須自己來確定需要恢復(fù)的SCN,然后使用DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(SCN_NUMBER); 來定位你的恢復(fù)時間點,下面是使用方法:
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默認的是精確到天,如果你不設(shè)定,像上面的例子你不會得到預(yù)期結(jié)果。
4.2 如果你使用sysdate和DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER來獲取時間點或者SCN值,你必須注意它們?nèi)〉枚际钱斍暗臅r間點和SCN值。
4.3 你只能在事務(wù)開始時進入閃回查詢模式,如果之前有DML操作,則必須COMMIT。
4.4 閃回查詢無法恢復(fù)到表結(jié)構(gòu)改變之前,因為閃回查詢使用的當前的數(shù)據(jù)字典。
您可能感興趣的文章:- oracle數(shù)據(jù)庫的刪除方法詳解
- Oracle刪除數(shù)據(jù)報ORA 02292錯誤的巧妙解決方法
- oracle 數(shù)據(jù)按主鍵刪除慢問題的解決方法
- Oracle刪除重復(fù)的數(shù)據(jù),Oracle數(shù)據(jù)去重復(fù)
- Oracle數(shù)據(jù)庫中的級聯(lián)查詢、級聯(lián)刪除、級聯(lián)更新操作教程
- 徹底刪除Oracle數(shù)據(jù)庫的方法
- oracle查詢重復(fù)數(shù)據(jù)和刪除重復(fù)記錄示例分享
- oracle數(shù)據(jù)庫添加或刪除一列的sql語句
- oracle 批量刪除表數(shù)據(jù)的幾種方法