您现在的位置: 万盛学电脑网 >> 程序编程 >> 数据库 >> oracle教程 >> 正文

oracle闪回版本和闪回事务查询详解

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   --- 说明闪回数据库

  --- 使用闪回表将表内容还原到过去的特定时间点

  --- 从删除表中进行恢复

  --- 使用闪回查询查看截止到任一时间点的数据库内容

  --- 使用闪回版本查询查看某一行在一段时间内的各个版本

  --- 使用闪回事务查询查看事务处理历史记录或行

  优点:

  闪回技术由于只能处理更改数据,所以从根本上改变了恢复技术。使用这个技术时,从错误中恢复花费的时间等于制造错误所花费的时间。当闪回技术使用时,它与介质恢复相比,在易用性、可用性和还原时间方面有明显的优势。

  闪回数据库使用闪回日志执行闪回。闪回删除使用回收站。其他所有功能都使用还原数据。

oracle闪回版本和闪回事务查询详解    三联

  闪回时间浏览

  闪回技术提供的功能可用于查询方案对象的过去版本、查询历史记录数据以及执行更改分析。每个事务处理在逻辑上都会生成新版本数据库。使用闪回技术,可通过浏览这些版本来查找错误以及原因。

  · 闪回查询:查询特定时间点的所有数据。

  · 闪回版本查询:查看两个时间之间行的所有版本已经更改了行的事务处理。

  · 闪回事务处理查询:查看事务处理做的所有更改。

  使用闪回查询功能时,可以对自特定时间起的数据库执行查询。通过使用select语句的 as of 子句,可指定要查看其数据的时间戳。这有助于分析数据差异。

  实验一:闪回查询

  实验一:闪回查询:as of timestamp

  SYS@ORCL>conn tyger/tyger

  Connected.

  TYGER@ORCL>create table fb_query as select * from scott.dept;

  Table created.

  TYGER@ORCL>select * from fb_query;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>set time on;

  09:51:36 TYGER@ORCL>delete fb_query where deptno=10;

  1 row deleted.

  09:51:53 TYGER@ORCL>commit;

  Commit complete.

  09:51:57 TYGER@ORCL>select * from fb_query;

  DEPTNO DNAME LOC

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

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  09:52:06 TYGER@ORCL>select * from fb_query as of timestamp sysdate-1/1440;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  实验二:闪回查询应用

  10:25:04 TYGER@ORCL>drop table fb_tyger purge;

  Table dropped.

  10:25:10 TYGER@ORCL>create table fb_tyger as select * from scott.dept;

  Table created.

  10:25:33 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  10:25:44 TYGER@ORCL>select sysdate from dual;

  SYSDATE

  ---------

  14-MAR-14

  10:26:02 TYGER@ORCL>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

  Session altered.

  10:26:30 TYGER@ORCL>select sysdate from dual;

  SYSDATE

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

  2014-03-14 10:26:38

  10:26:38 TYGER@ORCL>update fb_tyger set dname='';

  4 rows updated.

  10:26:51 TYGER@ORCL>commit;

  Commit complete.

  10:26:54 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  10:27:12 TYGER@ORCL>select * from fb_tyger as of timestamp to_timestamp('2014-03-14 10:26:38','yyyy-mm-dd hh24:mi:ss');

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  此处遇到错误:

  ERROR at line 1:

  ORA-01466: unable to read data - table definition has changed

  参考文档: http://blog.csdn.net/wanghui5767260/article/details/21227101

  10:29:21 TYGER@ORCL>select * from fb_tyger as of timestamp sysdate-3/1440;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  10:29:35 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  10:46:22 TYGER@ORCL>set time off

  TYGER@ORCL>update fb_tyger t

  2 set dname =

  3 (select dname from fb_tyger as of timestamp

  4 to_timestamp('2014-03-14 10:26:38','yyyy-mm-dd hh24:mi:ss')

  5 where t.deptno=fb_tyger.deptno);

  4 rows updated.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  实验三:闪回查询 as of scn

  TYGER@ORCL>conn / as sysdba

  Connected.

  SYS@ORCL>grant execute on dbms_flashback to tyger;

  Grant succeeded.

  TYGER@ORCL>select dbms_flashback.get_system_change_number from dual;

  GET_SYSTEM_CHANGE_NUMBER

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

  1107246

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>delete fb_tyger where deptno<=30;

  3 rows deleted.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  40 OPERATIONS BOSTON

  TYGER@ORCL>select * from fb_tyger as of scn 1107246;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  实验四:利用PL/SQL包dbms_flashback

  语法:

  · 会话启用闪回指定时间:

  DBMS_FLASHBACK.ENABLE_AT_TIME(query_time IN TIMESTAMP);

  · 会话启用闪回指定SCN:

  DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(query_scn IN NUMBER);

  · 关闭闪回:

  DBMS_FLASHBACK.DISABLE;

  TYGER@ORCL>conn / as sysdba

  Connected.

  SYS@ORCL>grant execute on dbms_flashback to tyger;

  Grant succeeded.

  SYS@ORCL>conn tyger/tyger

  Connected.

  TYGER@ORCL>

  TYGER@ORCL>

  TYGER@ORCL>

  TYGER@ORCL>create table fb_query1 as select * from scott.dept;

  Table created.

  TYGER@ORCL>create table fb_query2 as select * from scott.dept;

  Table created.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_query1;

  DEPTNO DNAME LOC

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

  10 ACCOUNTIN