闪回表
语法:
FLASHBACK TABLE [ schema. ] table [, [
schema. ] table ]...
TO { { { SCN | TIMESTAMP } expr| RESTORE
POINT restore_point}
[ { ENABLE | DISABLE } TRIGGERS ]|
BEFORE DROP [ RENAME TO table ]} ;
注 flashback table 为 ddl 命令
实验:
1) 创建测试表 fb_table,在表 fb_table 上创建触发器( trg_fb_table), fb_table 的 x 字段创建一个索引( idx_fb_table),此时,与 fb_table 相关的对象就有两个,一个是触发器,一个是索引,这三个对象的状态都是有效的。
SCOTT@ORA11GR2>create table fb_table(x number(2),d date);
Table created.
SCOTT@ORA11GR2>
SCOTT@ORA11GR2>create or replace trigger trg_fb_table before insert on fb_table for each row
2 begin
3 if :new.d is null then
4 :new.d := to_date('20080808','yyyymmdd');
5 end if;
6 end;
7 /
Trigger created.
SCOTT@ORA11GR2>create unique index idx_fb_table on fb_table(x);
Index created.
SCOTT@ORA11GR2>select object_name,status from user_objects where object_name like '%FB_TABLE';
OBJECT_NAME STATUS
--------------- -------
TRG_FB_TABLE VALID
IDX_FB_TABLE VALID
FB_TABLE VALID
1) 插入测试数据,验证触发器
SCOTT@ORA11GR2>insert into fb_table(x) values(1);
1 row created.
SCOTT@ORA11GR2>insert into fb_table(x,d) values(2,sysdate);
1 row created.
SCOTT@ORA11GR2>commit;
Commit complete.
SCOTT@ORA11GR2>select * from fb_table;
X D
---------- -------------------
1 2008-08-08 00:00:00
2 2016-09-29 20:33:27
SCOTT@ORA11GR2>
2) 记录当前系统时间
SCOTT@ORA11GR2>select sysdate from dual;
SYSDATE
-------------------
2016-09-29 20:35:55
SCOTT@ORA11GR2>delete fb_table where x=2;
1 row deleted.
SCOTT@ORA11GR2>commit;
Commit complete.
SCOTT@ORA11GR2>select * from fb_table;
X D
---------- -------------------
1 2008-08-08 00:00:00
--使用闪回查询进行闪回表前的查询验证:
SQL> select * from fb_table as of timestamp sysdate-1/1440;
X D
---------- -------------------
1 2008-08-08 00:00:00
2 2016-09-29 20:33:27
或者如下:
SQL> select * from fb_table as of timestamp to_date('2016-09-29 20:35:55','yyyy-mm-dd hh24:mi:ss');
X D
---------- -------------------
1 2008-08-08 00:00:00
2 2016-09-29 20:33:27
5) 尝试通过闪回表的方式恢复数据,第一次闪回表报错很明显,没有启用行移动,启用后,成功闪回表
SCOTT@ORA11GR2>flashback table fb_table to timestamp to_date('2016-09-29 20:35:55','yyyy-mm-dd hh24:mi:ss');
flashback table fb_table to timestamp to_date('2016-09-29 20:35:55','yyyy-mm-dd hh24:mi:ss')
*
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled
(行移动是针对表而言的,闪回表必须开启row movement)
——查询表fb_table是否启动了行移动:
SCOTT@ORA11GR2>select table_name,row_movement from user_tables where table_name='FB_TABLE';
TABLE_NAME ROW_MOVE
------------------------------ --------
FB_TABLE DISABLED
SCOTT@ORA11GR2>
——开启表fb_table的行移动功能:
SCOTT@ORA11GR2>alter table fb_table enable row movement;
Table altered.
SCOTT@ORA11GR2>select table_name,row_movement from user_tables where table_name='FB_TABLE';
TABLE_NAME ROW_MOVE
------------------------------ --------
FB_TABLE ENABLED
——闪回表操作:
SCOTT@ORA11GR2>flashback table fb_table to timestamp to_date('2016-09-29 20:35:55','yyyy-mm-dd hh24:mi:ss');
Flashback complete.
SCOTT@ORA11GR2>select * from fb_table;
X D
---------- -------------------
1 2008-08-08 00:00:00
2 2016-09-29 20:33:27
6) 查看与测试表相关对象
所有对象的状态均为有效的,看来,测试的时候我们创建触发器和索引意义不大了,不过要说明的是,
11g 在闪回表上做了升级, 10g 闪回表后,触发器为失效的状态。
SCOTT@ORA11GR2>select object_name,status from user_objects where object_name like '%FB_TABLE';
OBJECT_NAME STATUS
--------------- -------
TRG_FB_TABLE VALID
IDX_FB_TABLE VALID
FB_TABLE VALID
7) 当然,我们也可以通过以下方式进行闪回表
--基于时间闪回表
SCOTT@ORA11GR2>flashback table fb_table to timestamp sysdate - 18/1440;
Flashback complete.
SCOTT@ORA11GR2>select * from fb_table;
X D
---------- -------------------
1 2008-08-08 00:00:00
2 2016-09-29 20:33:27
(闪回的时间是按你删除操作前的时间和你执行闪回表时的时间时间差来定的,可以提前设置set time on显示时间)
或者:
SCOTT@ORA11GR2>flashback table fb_table to TIMESTAMP sysdate - interval '5' minute;
闪回到5分钟之前,另一种命令写法:flashback table fb_table to TIMESTAMP sysdate-5/1440;
Flashback complete.
SCOTT@ORA11GR2>select * from fb_table;
X D
---------- -------------------
1 2008-08-08 00:00:00
2 2016-09-29 20:33:27
--单张表基于 SCN 闪回表
SYS@ORA11GR2>
SCOTT@ORA11GR2>flashback table fb_table to scn 900332;
Flashback complete.
SCOTT@ORA11GR2>
--多张表基于 SCN 闪回表
SCOTT@ORA11GR2>flashback table fb_table,dept to scn 900332;
Flashback complete.