如果哪天資料不小心砍了....

還好有方式可以救回來

開始..........

SCN(系統改變號),它的英文全拼為:System Change Number ,它是數據庫中非常重要的一個數據結構。

SCN提供了Oracle的內部時鐘機制,可被看作邏輯時鐘,這對於恢復操作非常重要的。註釋:Oracle 僅根據 SCN 執行恢復。

接下來是直接實作了:

取得SCN號(在PL/SQL上面是跑不出來低,用的帳號權限不夠也沒用)

select dbms_flashback.get_system_change_number from dual;

請用下列方式

select timestamp_to_scn(to_timestamp('2010-12-03 23:47:46','YYYY-MM-DD HH24:MI:SS')) from dual;

結果:6411415406

這就是在2010/12/3時產生的序號)

再來直接完整範例:

一、 建一個Table

create table test_file

(

a number,

b varchar2(20);

)

二、寫一些資料進去

insert into test_file (a,b) values (1,'A');

insert into test_file (a,b) values (2,'B');

insert into test_file (a,b) values (3,'B');

2010120601.jpg

三、查SCN號

select timestamp_to_scn(to_timestamp('2010-12-03 22:19:00','YYYY-MM-DD HH24:MI:SS')) from dual;

結果:6411455406

2010120602.jpg

四、用查出序號確認筆數或資料內容

select count(*) from test_file as of scn 6411415406;

select * from test_file as of scn 6411415406;

五 、先刪除剛剛寫入的那三筆資料(注意刪除的時間)

delete from test_file

六、再查一次刪除後的SCN;

select timestamp_to_scn(to_timestamp('2010-12-03 23:51:00','YYYY-MM-DD HH24:MI:SS')) from dual;

結果:6411841278

2010120603.jpg

七、用剛查出SCN確認是不是還有資料(應該被刪除了所以是0筆)

select count(*) from test_file as of scn 6411841278

2010120604.jpg

八、恐怖的來了….要恢復剛剛刪掉的東東了

insert into test_file select * from test_file AS OF SCN 6411415406; <=這個SCN號就是剛剛查出還是正常的時間的

最後再查詢看看資料是不是被恢復了

select * from test_file

哇…恭喜恭喜還好有恢復成功..不然就………

恢復還有方式二(先建一個新的Table寫進去再去比對二個Table資料)

因為如果資料是一直變動的那就很麻煩了~

create table restore_test as select * from test_file AS OF SCN 6411415406;

參考:

http://jlins.javaeye.com/blog/578837

http://m363796275.blog.hexun.com.tw/43385543_d.html

arrow
arrow
    全站熱搜

    鴨爸 發表在 痞客邦 留言(0) 人氣()