oracle誤刪除的數(shù)據(jù)怎么恢復(fù)?下面為大家?guī)韔racle恢復(fù)刪除的數(shù)據(jù)方法:
PART1
分為兩種方法:scn和時間戳兩種方法恢復(fù)。
一、通過scn恢復(fù)刪除且已提交的數(shù)據(jù)
1、獲得當(dāng)前數(shù)據(jù)庫的scn號
select current_scn from v$database; (切換到sys用戶或system用戶查詢)
查詢到的scn號為:1499223
2、查詢當(dāng)前scn號之前的scn
select * from 表名 as of scn 1499220; (確定刪除的數(shù)據(jù)是否存在,如果存在,則恢復(fù)數(shù)據(jù);如果不是,則繼續(xù)縮小scn號)
3、恢復(fù)刪除且已提交的數(shù)據(jù)
flashback table 表名 to scn 1499220;
二、通過時間恢復(fù)刪除且已提交的數(shù)據(jù)
1、查詢當(dāng)前系統(tǒng)時間
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
2、查詢刪除數(shù)據(jù)的時間點的數(shù)據(jù)
select * from 表名 as of timestamp to_timestamp('2013-05-29 15:29:00','yyyy-mm-dd hh24:mi:ss'); (如果不是,則繼續(xù)縮小范圍)
3、恢復(fù)刪除且已提交的數(shù)據(jù)
flashback table 表名 to timestamp to_timestamp('2013-05-29 15:29:00','yyyy-mm-dd hh24:mi:ss');
注意:如果在執(zhí)行上面的語句,出現(xiàn)錯誤。可以嘗試執(zhí)行 alter table 表名 enable row movement; //允許更改時間戳
PART2
SCN(系統(tǒng)改變號),它的英文全拼為:System Change Number ,它是數(shù)據(jù)庫中非常重要的一個數(shù)據(jù)結(jié)構(gòu)。
SCN提供了Oracle的內(nèi)部時鐘機制,可被看作邏輯時鐘,這對于恢復(fù)操作是至關(guān)重要的
注釋:Oracle 僅根據(jù) SCN 執(zhí)行恢復(fù)。
它定義了數(shù)據(jù)庫在某個確切時刻提交的版本。在事物提交時,它被賦予一個唯一的標(biāo)示事物的SCN 。一些人認(rèn)為 SCN 是指, System Commit Number ,而通常 SCN 在提交時才變化,所以很多情況下,
這兩個名詞經(jīng)常被交替使用。
究竟是哪個詞其實對我們來說并不是最重要的,重要的是我們知道 SCN 是 Oracle 內(nèi)部的時鐘機制, Oracle 通過 SCN 來維護(hù)數(shù)據(jù)庫的一致性,并通過SCN 實施 Oracle 至關(guān)重要的恢復(fù)機制。
具體執(zhí)行流程我們可從以下幾個示例圖中體會;
1.原表記錄 $ sqlplus eygle/eygle
SQL*Plus: Release 10.1.0.2.0 - Production on Wed Mar 30 08:52:04 2005
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
SQL>select count(*) from t_officename;
COUNT(*)
----------
9318
2.誤刪除所有記錄
并且提交更改。
SQL>delete from t_officename;
9318 rows deleted.
SQL>commit;
Commit complete.
SQL>select count(*) from t1;
COUNT(*)
----------
0
3.獲得當(dāng)前SCN
如果能夠確切知道刪除之前SCN最好,如果不知道,可以進(jìn)行閃回查詢嘗試.
SQL>select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
10671006
SQL>select count(*) from t1 as of scn 10671000;
COUNT(*)
----------
0
SQL>select count(*) from t1 as of scn 10670000; //如果找不到,不斷減少scn號再償試
COUNT(*)
----------
9318
我們看到在SCN=10670000時,數(shù)據(jù)都在。
4.恢復(fù)數(shù)據(jù).
SQL>insert into t_officename select * from t_officename as of scn 10670000;
9318 rows created.
SQL>commit;
Commit complete.
SQL>select count(*) from t_officename;
COUNT(*)
----------
9318
文章2
誤刪數(shù)據(jù)后的還原
select timestamp_to_scn(to_timestamp('2009-03-13 09:00:00','YYYY-MM-DD HH:MI:SS')) from dual;
結(jié)果:13526973
將刪除時間轉(zhuǎn)換為scn
select * from reportinfo AS OF SCN 13526973
將reportinfo表中的scn點的數(shù)據(jù)取出
然后可以根據(jù)這個數(shù)據(jù)進(jìn)行還原操作
create table reporttest as select * from reportinfo where 1=0;
insert into reporttest select * from reportinfo AS OF SCN 13526973;
--上面兩句應(yīng)該可以合成一句
--create table reporttest as select * from reportinfo AS OF SCN 13526973;
這是reporttest表中就是scn點的reportinfo數(shù)據(jù).處理即可