一点介绍:
oracle从10g开始,提供了闪回功能,即flashbask
根据oracle官方文档,flashback功能又分为五种:
1:flashback database (利用闪回日志)
2:flashback drop (利用回收站:recyclebin)
3:flashback table (利用undo)
4:flashback query (利用undo)
5:flashback version query (利用undo)
6:flashback transaction query (利用undo)
flashback drop 是针对数据库表的操作
flashback table 是针对表里的dml操作,即:insert、update、delete
flashback query 是针对select查询操作
flashback version query和flashback transaction query联合起来,可以查看到某个事物在某个时间段内对行的操作,并记录事物修改的sql语句,给管理员提供分析诊断信息
下面先简单测试一下 flashback table 和flashback query操作
OS: redhat linux as4 u2
DB: oracle10gr2
C:\>sqlplus
system/sysdba@ora10hhaSQL*Plus: Release 10.1.0.2.0 - Production on 星期三 6月 29 16:59:24 2011
Copyright (c) 1982, 2004, Oracle. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL>
SQL>
SQL>
SQL> show user;
USER 为 "SYSTEM"
SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 xx xx
SQL>
SQL> insert into scott.dept values(60,'k','c');
已创建 1 行。
SQL> commit;
提交完成。
SQL>
SQL>
SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 xx xx
60 k c
已选择6行。
SQL>
SQL>
SQL>
SQL>
确定当前系统scn值,以便设置恢复的起点SQL> select current_scn from v$database;
CURRENT_SCN
-----------
829848
SQL>
此时模拟不小心删掉表里的一条数据,并已经做了提交SQL> delete from scott.dept where deptno=60;
已删除 1 行。
SQL> commit;
提交完成。
正常查询的话,scott用户下的dept表里已经没有了deptno为60的数据SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 xx xx
此时利用数据库的闪回查询,即flashback query 功能,可以查看到删除之前的那个点上的值SQL> select * from scott.dept as of scn 829848;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 xx xx
60 k c
已选择6行。
flashback table需要激活行移动功能SQL> flashback table scott.dept to scn 829848;
flashback table scott.dept to scn 829848
*
第 1 行出现错误:
ORA-08189: cannot flashback the table because row movement is not enabled
SQL> alter table scott.dept enable row movement;
表已更改。
flashback tableSQL> flashback table scott.dept to scn 829848;
闪回完成。
SQL>
SQL>
可以看到表已经闪回到了以前那个scn点上SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 xx xx
60 k c
已选择6行。
SQL>
总结: 对于oracle10g的版本而言,如果现网中不小心对表进行了dml操作,并且事物已经提交,可以用flashback table功能进行恢复。闪回时,既可以用scn,也可以用timestamp