Oracle 10 闪回功能实例讲解 联系客服

发布时间 : 星期日 文章Oracle 10 闪回功能实例讲解更新完毕开始阅读7281d666783e0912a2162aaf

create restore point one; --创建闪回点one insert into tb_emp --插入deptno为20的员工 select empno,ename,job,deptno from scott.emp where deptno=20; commit;

create restore point two; --创建闪回点two insert into tb_emp --插入deptno为30的员工 select empno,ename,job,deptno from scott.emp where deptno=30; commit;

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 restore point two; --闪回到闪回点two之前 select deptno,count(*) from tb_emp group by deptno order by 1; flashback table tb_emp to restore point one; --闪回到闪回点one之前

select deptno,count(*) from tb_emp group by deptno order by 1;

DEPTNO COUNT(*)

10 3

---------- ----------

drop restore point two; --删除创建的闪回点 drop restore point one; drop restore point zero;

5.存在参照关系的表闪回

帐户flasher中表tb1与表tb2存在外键关系,表tb1的deptno 参照了表tb2中的d帐户flasher中表tb1与scott.emp具有相同的表结构,表tb2与表scott.dept具有下面为表tb2新增一个deptno,且为表tb1新增一条记录

eptno列 相同的表结构

create table tb1 --基于表scott.emp来创建表tb1

enable row movement as select * from scott.emp ;

create table tb2 --基于表scott.dept来创建表tb2 enable row movement as select * from scott.dept;

alter table tb1 --为表tb1添加主键约束 add constraint tb1_empno_pk primary key(empno); alter table tb2 --为表tb2添加主键约束 add constraint tb2_deptno_pk primary key(deptno); alter table tb1 --为表tb1添加外键约束 add constraint tb1_tb2_deptno_fk foreign key(deptno) references tb2(deptno);

insert into tb2 --为表tb2插入一个新部门 select 50,'Customer','Landon' from dual;

insert into tb1(empno,ename,job,deptno) --为表tb1插入一个新的雇员 select 8000,'Robinson','Clerk',50 from dual; commit;

select current_scn from v$database; --获得当前的scn --- 687444

delete from tb1 where empno=8000; --删除先前新增的部门 delete from tb2 where deptno=50; --删除先前新增的雇员 commit;

flashback table tb1 to scn 687444; --闪回先前删除的雇员 /*

ERROR at line 1:

ORA-02091: transaction rolled back --提示事务被回滚,外键没有找到 ORA-02291: integrity constraint (FLASHER.TB1_TB2_DEPTNO_FK) violated -

parent key not found */

flashback table tb1,tb2 to scn 687444; --将两个表同时闪回

select empno,ename,deptno,dname --此时新增的雇员被闪回,部门也from tb1 inner join tb2 using(deptno) where deptno=50;

EMPNO ENAME DEPTNO DNAME 8000 Robinson 50 Customer

被闪回

---------- ---------- ---------- --------------

6.表闪回的几个常见问题

a.当闪回删除操作之前,如果某个键值如主键被重用,将导致违反主键约束,闪回失b.若闪回所需要的UNDO信息不存在,将引发ORA-08180:no snapshot found based c.如果受闪回影响的记录被其它用户锁定,将引发ORA-00054:resource busy and ad.表定义在闪回期间不能发生变化,否则导致ORA-01466:unable to read data - te.闪回前未启用row movement,将收到ORA-08189: cannot flashback the table bef.对于存在参照关系的情况,建议将主表等一起实施闪回,否则,将收到ORA-02091: g.SYS 模式中的表不能使用表闪回技术

败。

on specified time(未找到基于指定时间的快照)错误 cquire with NOWAIT specified (资源忙碌)错误 able definition has changed(表定义已变化)错误 cause row movement is not enabled 错误 transaction rolled back,ORA-02291错误

7.有关ORA-01466的问题可以参考这里:Flashback与ORA-01466

-- Oracle 闪回特性(Flashback Version、Flashback Transaction) --==========================================================

Oracle闪回特性为数据的快速回复某一对象的特定数据提供了更多的便利。前面介绍了闪flashback drop ,flashback query ,flashback table 。接下来本文将介绍Flashback

回的几种特性,包括flashback database, Version与Flashback Transaction。

一、Flashback Version Query(闪回版本查询)

闪回版本查询指的是Oracle可以针对特定的对象来查询某一特定段内该对象的变化的所有据特定的需要来将该对象修正到特定的时刻。闪回版本查询同闪回查询,闪回表一样,同,当UNDO段的数据由于空间压力而被清除,则产生无法闪回的情况。 情况,可以对此跟踪该对象的变更情况。也可以根 样是使用了UNDO段的数据,即数据变更的多次镜像

1.闪回版本查询语法,使用VERSIONS BETWEEN 关键字

SELECT

FROM

VERSIONS BETWEEN SCN AND --基于SCN的版本查询 [WHERE ]

[GROUP BY ] [HAVING

[ORDER BY ]

SELECT

FROM

VERSIONS BETWEEN timestamp to_timestamp('start_timestamp') and to_timestam[WHERE ]

[GROUP BY ] [HAVING

[ORDER BY ]

p('end_timestamp') --基于TIMESTAMP的版本查询

2.创建演示环境

--对表tb1作如下操作,插入empno为的记录后,更新其职务,然后再删除该记录,

flasher@ORCL>create table tb1 tablespace users as select empno,ename,j

最后再次插入该记录

ob,deptno from scott.emp; --创建表tb1

flasher@ORCL> insert into tb1 values(1000,'Jack','Clerk',20); --插入

记录

flasher@ORCL> commit; --提交事务

flasher@ORCL> update tb1 set job='Manager' where empno=1000; --将职

务更新为Manager

flasher@ORCL> commit; --提交事务

flasher@ORCL> delete from tb1 where empno=1000; --删除该记录 flasher@ORCL> commit; --提交事务

flasher@ORCL> insert into tb1 values(1000,'Jack','President',20); --

重新插入该记录

flasher@ORCL> commit;

--提交事务