標籤:oracle drop delete
Oracle中Drop,delete,truancate表恢複
oracle中,常常會由於一些失誤導致表的刪除,以下是我寫的一些表恢複的方法。
閃回模式得滿足條件(啟用閃回區和啟用歸檔):
1.檢查是否啟動了flash recovery area
show parameter db_recovery_file
2.檢查是否啟用了歸檔
archive log list;
(一)Drop表的恢複
如果按照平時刪除表的方法:(Drop table tablename;)的話。表不會立即被刪除,而是存放於資源回收筒中(Oracle 10g以後才有)。我們可以通過閃回(Flashback)把表恢複。
查看被資源回收筒中刪除的表:
Select * from dba_recyclebin (where type=‘TABLE‘); --找出對應的表是否存在
閃回表:
Flashback tbale 表名 to before drop;
**資源回收筒存放的不僅僅只有表,還有其他對象
(二)Delete表的恢複
同樣,Delete後可以通過閃回時間戳記或者閃回SCN號進行恢複。
基於時間戳記恢複
①確定刪除資料的時間(在刪除資料之前的時間就行,不過最好是刪除資料的時間點)
②用以下語句找出刪除的資料:
select * from 表名 as of timestamp to_timestamp(‘刪除時間點‘,‘yyyy-mm-dd hh24:mi:ss‘)
③開啟行移動功能
alter table 表名 enable row movement
④把刪除的資料重新插入原表:
insert into 表名 (select * from 表名 as of timestamp to_timestamp(‘刪除時間點‘,‘yyyy-mm-dd hh24:mi:ss‘));注意要保證主鍵不重複。
⑤關閉行移動功能 ( 千萬別忘記 )
alter table 表名 disable row movement
基於SCN號恢複
①擷取當前的SCN號
select dbms_flashback.get_system_change_number from dual; --假設得到scn號為10672000
②如果知道刪除前的SCN號最好,如果不知道就通過閃回查詢嘗試.
select count(*) from 表名 as of scn 10671000; --如果資料量為0,則繼續提前scn號
select count(*) from 表名 as of scn 10670000; --查詢的資料量為原來的資料量,則ok
③通過查詢的SCN號進行閃回恢複
insert into 表名 select * from tablename as of scn 10670000;
(三)Truncate表的恢複
truncate表後不會產生日誌記錄和復原段空間的使用,不能用查詢閃回恢複。
①最簡單的方法:下載使用PRM (資料庫災難恢複工具)
②進行資料庫閃回(在生產庫不推薦):
select sysdate from dual; --查看時間戳記,確認已經開啟閃回,如果沒開啟,只能用①方法了;
shutdown immediate; --關閉資料庫
startup mount; --啟動到mount狀態
flashback database to timestamp to_timestamp(‘2017-07-30 18:22:33‘,‘yyyy-mm-dd hh24:mi:ss‘); --閃回刪除時間點的時間
alter database open; --開啟資料庫
Oracle中Drop,Delete,Truancate表恢複