在Oracle的數據庫中,如果不小心刪除數據,該如何恢復數據呢?
有兩種方法 :scn 方法和時間戳方法
一、恢復刪除數據的SQL語法(建議用時間戳)
1、通過scn恢復刪除且已提交的數據
1)獲得當前數據庫的scn號
select current_scn from v$database; (切換到sys用戶或system用戶查詢)
查詢到的scn號為:1499223
2)查詢當前scn號之前的scn
select * from 表名 as of scn 1499220; (確定刪除的數據是否存在,如果存在,則恢復數據;如果不是,則繼續縮小scn號)
3)恢復刪除且已提交的數據
flashback table 表名 to scn 1499220;
2、通過時間恢復刪除且已提交的數據
1)查詢當前系統時間
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
2)查詢刪除數據的時間點的數據
select * from 表名 as of timestamp to_timestamp('2018-10-09 15:29:00','yyyy-mm-dd hh24:mi:ss'); (如果不是,則繼續縮小范圍)
3)恢復刪除且已提交的數據
--開啟行移動功能(解決執行以下語句報錯問題)
alter table 表名 enable row movement;
--恢復某個時間點的數據
flashback table 表名 to timestamp to_timestamp('2018-10-09 15:29:00','yyyy-mm-dd hh24:mi:ss');
--關閉行移動功能
alter table 表名 disable row movement;
二、恢復刪除數據的實例(方法)
1、查詢刪除數據前表數據
--查詢刪除前表的數據--- select * from Dxc_Goods;
2、執行刪除數據操作(132,133),並查看
--執行刪除操作:132,133-- delete from Dxc_GOODS where MID in(132,133); --提交(模擬誤刪操作) commit;
查看結果
3、恢復刪除並提交的數據 (指定刪除時間點,保證這個是執行刪除之前的時間)
--開啟行移動功能(解決執行以下語句報錯問題) alter table Dxc_Goods enable row movement; --恢復某個時間點的數據 flashback table Dxc_Goods to timestamp to_timestamp('2019-07-24 18:00:00','yyyy-mm-dd hh24:mi:ss'); --關閉行移動功能 alter table Dxc_Goods disable row movement;
執行后,查詢數據(132,133數據已恢復)
PS:
參考網址:https://blog.csdn.net/qq_36460189/article/details/82983732