使用flashback恢复数据

一、flashback query

先查询一下当前的SCN,这样好回退。

select dbms_flashback.get_system_change_number from dual;
select current_scn from v$database;

适应范围:表中的数据持续变化,需要看到某个时间点错误删除修改了某些记录,可以根据这些记录再进行数据恢复。

1恢复被删除了的储存过程:
select *
  from dba_source AS OF TIMESTAMP TO_TIMESTAMP('2015-08-24 12:00:00', 'YYYY-MM-DD HH24:MI:SS')
 where name = 'xxxxx'
   and owner = 'xxxx'
 order by line;

 

2恢复表数据:

例如:删除了某些条件的数据,可以这样恢复。

把删除的数据恢复到中间表里,然后insert进原表。也可以不带where条件,让开发人员自行选择有效数据insert进原表。

create table flashback_qry as
select *
  from lar_surp_ulife_detail as of timestamp to_Date('2017-08-17 09:40:00', 'yyyy-mm-dd hh24:mi:ss')
 where proc_date = date '2017-7-31';


insert into lar_surp_ulife_detail select * from flashback_qry ;
commit;

 

--闪回到15分钟前

select *  from orders   as of timestamp (systimestamp - interval '15' minute)   where ......

这里可以使用DAY、SECOND、MONTH替换minute,例如:

SELECT * FROM orders AS OF TIMESTAMP(SYSTIMESTAMP - INTERVAL '2' DAY)

--闪回到某个时间点

select  *   from orders   as of timestamp   to_timestamp ('01-Sep-04 16:18:57.845993', 'DD-Mon-RR HH24:MI:SS.FF') where ...

--闪回到两天前

select * from orders   as of timestamp (sysdate - 2) where.........

 

二、FLASHBACK TABLE

1、表已经drop掉之后使用,可以快速恢复。

flashback table orders to before drop;

如果drop的表已经重新建立了一个同名称的表,那么需要加上rename to子句。

flashback table order to before drop   rename to order_old_version;

2、表的数据错误删除或修改后,没有后续数据变化,可以快速恢复。

第一步,首先要启用行迁移

alter table order enable row movement;

第二步,闪回表

到15分钟前:

flashback table order   to timestamp systimestamp - interval '15' minute;

到某个时间点:

FLASHBACK TABLE order TO TIMESTAMP    TO_TIMESTAMP('2007-09-12 01:15:25 PM','YYYY-MM-DD HH:MI:SS AM')

例1,回滚tables:
select *  from l_code_map_mas   as of timestamp to_timestamp ('2016-11-11 8:00:00.00', 'yyyy-mm-dd HH24:MI:SS.FF')

alter table TLOANDATA.l_code_map_mas enable row movement;

flashback table TLOANDATA.l_code_map_mas   to timestamp  to_timestamp('2016-11-11 8:00:00.00', 'yyyy-mm-dd HH24:MI:SS.FF');

你可能感兴趣的:(oracle)