发布时间 : 星期日 文章Oracle 10 闪回功能实例讲解更新完毕开始阅读7281d666783e0912a2162aaf
flasher@ORCL11G> select current_scn from v$database; CURRENT_SCN -----------
2032782
flasher@ORCL11G> select * from tb1;
EMPNO ENAME JOB DEPTNO 7369 Henry CLERK 20 7566 JONES MANAGER 20
---------- ---------- --------- ----------
flasher@ORCL11G> delete from tb1 where empno=7369; flasher@ORCL11G> commit;
flasher@ORCL11G> select * from tb1 as of scn 2032782;
EMPNO ENAME JOB DEPTNO 7369 Henry CLERK 20 7566 JONES MANAGER 20
---------- ---------- --------- ----------
由以上可知,通过闪回查询获得所需的记录信息,然后来构造新的DML语句并实施其
操作来保证数据的完整性
二、Flashback Table Query(闪回表查询)
FLASHBACK TABLE
通过查询UNDO段来抽取所有已变化的记录细节,在此基础之上再构造和执行能够倒退这些表闪回通过执行倒退变化的语句并且该执行是一个事务,所有常用规则在该事务上起作用。 表闪回时,表上的触发器缺省被禁用,即该表上的DML触发器将暂时失效,可以在闪回时表闪回需要启用表上的记录转移选项 变化的语句
指定触发器是否失效。
1.下面给出表闪回的种方式
FLASHBACK TABLE
FLASHBACK TABLE
TO TIMESTAMP
TO RESTORE POINT
2.演示基于SCN的表闪回
下面的演示首先创建表tb_tables,并对表分几次插入数据,在完成插入前记录其SCN
号用于后续对其进行闪回
create table tb_emp as --创建演示表tb_emp select empno,ename,job,deptno from scott.emp where 1=0;
select table_name,row_movement from user_tables; --查看表的row movemen
t行为,缺省为disable
TABLE_NAME ROW_MOVE ------------------------------ -------- TB_EMP DISABLED
select current_scn,systimestamp from v$database; --获取系统当前的SCN CURRENT_SCN SYSTIMESTAMP
----------- --------------------------------------
661490 01-JAN-11 10.56.28.733000 PM +08:00
insert into tb_emp --插入deptno为10的员工
select empno,ename,job,deptno from scott.emp where deptno=10; commit;
select current_scn,systimestamp from v$database; --获取系统当前的SCN CURRENT_SCN SYSTIMESTAMP
----------- --------------------------------------
661510 01-JAN-11 10.56.56.546000 PM +08:00
insert into tb_emp --插入deptno为20的员工
select empno,ename,job,deptno from scott.emp where deptno=20; commit;
select current_scn,systimestamp from v$database; --获取系统当前的SCN CURRENT_SCN SYSTIMESTAMP
----------- --------------------------------------
661521 01-JAN-11 10.57.17.358000 PM +08:00
insert into tb_emp --插入deptno为30的员工
select empno,ename,job,deptno from scott.emp where deptno=30; commit;
select current_scn,systimestamp from v$database; --获取系统当前的SCN CURRENT_SCN SYSTIMESTAMP
----------- --------------------------------------
661539 01-JAN-11 10.57.37.843000 PM +08:00
select deptno,count(*) from tb_emp group by deptno order by 1;
DEPTNO COUNT(*)
10 3 20 5 30 6
---------- ----------
flashback table tb_emp to scn 661521; --将表闪回到scn为,即插入部
门号为的记录之前
flashback table tb_emp to scn 661521 --闪回失败,收到错误提示,没
*
有开启row movement
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enab
led
alter table tb_emp enable row movement; --开启表tb_emp表的row movem
ent 功能
flashback table tb_emp to scn 661521; --再次实施闪回,闪回成功 select deptno,count(*) from tb_emp group by deptno order by 1; --记录
DEPTNO COUNT(*)
10 3 20 5
中没有部门为30的记录
---------- ----------
flashback table tb_emp to scn 661510; --将表闪回到scn为,即插入部
门号为20的记录之前
select deptno,count(*) from tb_emp group by deptno order by 1; --记录
中没有部门为20的记录
DEPTNO COUNT(*)
10 3
---------- ----------
3.演示基于TIMESTAMP的表闪回
使用to timestamp进行表闪回,继续使用上面创建的表来进行闪回
--使用timestamp将表闪回到插入部门号10为之前
flashback table tb_emp to timestamp to_timestamp('01-JAN-11 10.56.28.
733000');
flashback table tb_emp to timestamp to_timestamp('01-JAN-11 10.56.28.
*
733000') --收到错误提示
ERROR at line 1:
ORA-01466: unable to read data - table definition has changed --表结
构发生改变
flasher@ORCL11G> flashback table tb_emp to scn 661539; --可以将表闪回
到插入部门号为30的记录之后
Flashback complete.
此处演示中收到了错误提示,注意对于表闪回,可以多次使用同一类型的闪回方式,但交叉闪回则提示表定义发生了变化。闪回失败。我们可以再次创建一张类似的新表是,此处使用了timestamp,此演示在此省略
基于RESTORE POINT的表闪回首先要创建适当的闪回点,创建闪回点的方式为
CREATE RESTORE POINT point_name; DROP RESTORE POINT point_name
对于闪回成功之后,无用的闪回点可以及时删除掉,删除闪回点的方式为 下面对基于RESTORE POINT 闪回进行演示
可以往前闪回,一旦往前闪回之后,也可以往后进行闪回。 进行基于timestamp进行闪回,与闪回SCN说不同的
4.演示基于RESTORE POINT的表闪回
drop table tb_emp purge; --删除先前创建的表tb_emp create table tb_emp --创建演示表tb_emp enable row movement
as select empno,ename,job,deptno from scott.emp where 1=0; create restore point zero; --创建闪回点zero insert into tb_emp --插入deptno为10的员工 select empno,ename,job,deptno from scott.emp where deptno=10; commit;