Oracle Flashback之Flashback Transaction Query

Flashback Transaction Query

Flashback Transaction Query也是使用UNDO信息来实现。利用这个功能可以查看某个事务执行的所有变化,它需要访问flashback_transaction_query 视图,这个视图的XID列代表事务ID,利用这个ID可以区分特定事务发生的所有数据变化。

示例:
SQL> create table T (id int,name varchar2(30));

Table created.

SQL> insert into T VALUES(1,'Test1');

1 row created.

SQL> insert into T VALUES(2,'Test2');

1 row created.

SQL> insert into T VALUES(3,'Test3');

1 row created.

SQL> update T set name='Test11' WHERE ID=1;

1 row updated.

SQL> COMMIT;

Commit complete.

SQL> delete from T where id=2;

1 row deleted.

SQL> commit;

Commit complete.

Flashback transaction query:

 

SQL>  select xid,operation, undo_sql  from flashback_transaction_query where table_name='T' and table_owner='HR' order by start_timestamp desc;

XID              OPERATION  UNDO_SQL
---------------- ---------- --------------------------------------------------------------------------------
0600160014010000 DELETE     insert into "HR"."T"("ID","NAME") values ('2','Test2');
04001800DE000000 INSERT     delete from "HR"."T" where ROWID = 'AAAM1TAAEAAAAGEAAC';
04001800DE000000 INSERT     delete from "HR"."T" where ROWID = 'AAAM1TAAEAAAAGEAAA';
04001800DE000000 UPDATE     update "HR"."T" set "NAME" = 'Test1' where ROWID = 'AAAM1TAAEAAAAGEAAA';
04001800DE000000 INSERT     delete from "HR"."T" where ROWID = 'AAAM1TAAEAAAAGEAAB';

 

根据每个事务对应的XID来查看UNDO_SQL
SQL> select operation, undo_sql from flashback_transaction_query where xid=hextoraw('0600160014010000');

OPERATION  UNDO_SQL
---------- --------------------------------------------------------------------------------
DELETE     insert into "HR"."T"("ID","NAME") values ('2','Test2');

 

可以看到,Flashback Transaction Query主要用于审计一个事务,并可以回滚一个已经提交的事务。如果确定出错的事务是最后一个事务,我们利用Flashback Table或者Flashback Query就可以解决问题。但是,如果执行了一个错误的事务之后,又执行了一系列正确的事务,那么上面的方法就无能为力,利用Flashback Transaction Query可以查看或回滚这个错误的事务。

你可能感兴趣的:(Oracle Flashback之Flashback Transaction Query)