闪回事务查询(Flashback transcation query)模拟测试

Flashback transcation query 能够查询事务对表所做的操作,通过UNDO SQL可以取消对事务所做的修改。查询FLASHBACK_TRANSACTION_QUERY这个数据字典表来获取字典事务ID的信息.
Flashback Transaction Query实际上是查询的数据字典flashback_transaction_query。可以根据flashback_transaction_query 的undo_sql列值返回数据以前版本
flashback_transaction_query 列说明:

SQL> desc flashback_transaction_query
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 XID                                                       RAW(8)                 --事务ID
 START_SCN                                          NUMBER                --事务起始SCN,即第一个dml的SCN
 START_TIMESTAMP                             DATE                      --事务其实时间戳,即第一个dm的时间戳
 COMMIT_SCN                                      NUMBER                --提交事务时的SCN
 COMMIT_TIMESTAMP                         DATE                      -- 提交事务时的时间戳
 LOGON_USER                                      VARCHAR2(30)       --本次事务的用户
 UNDO_CHANGE#                                NUMBER                --撤销SCN
 OPERATION                                         VARCHAR2(32)       --执行的dml操作:DELETE,INSERT,UPDATE,BEGIN,UNKNOWN
 TABLE_NAME                                       VARCHAR2(256)     --dml更改的表
 TABLE_OWNER                                     VARCHAR2(32)      --表的所有者
 ROW_ID                                                VARCHAR2(19)       --修改行的ROWID
 UNDO_SQL                                           VARCHAR2(4000)   -?-撤销dml的sql语句

--使用闪回事务查询前,必须启用重做日志流的其他日志记录,重做日志流与Log Miner使用的数据相同,只是接口不同。
例子:
SQL> sqlplus / as sysdba

SQL> alter database add supplemental log data;

Database altered.

SQL> alter database add supplemental log data (primary key) columns;

Database altered.

SQL> grant select any transaction to scott;

Grant succeeded.

SQL> update xyc_t1 set sal=9999 where empno=7902;     --更改值sal=9999

1 row updated.

SQL> commit;

Commit complete.

SQL> select to_date(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;

TO_DATE(SYSDATE,'YY
-------------------
20210121 17:29:46

SQL> update xyc_t1 set sal=99999 where empno=7902;  --更改值sal=99999

1 row updated.

SQL> commit;

Commit complete.

SQL> select to_date(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;

TO_DATE(SYSDATE,'YY
-------------------
20210121 17:30:17

select versions_startscn,versions_starttime,versions_endscn,versions_endtime,versions_xid,versions_operation,empno
from xyc_t1 versions between timestamp to_timestamp('20210121 17:29:46','YYYY-MM-DD HH24:MI:SS') and to_timestamp('20210121 17:30:17','YYYY-MM-DD HH24:MI:SS');

VERSIONS_STARTSCN VERSIONS_STARTTIME                                                          VERSIONS_ENDSCN VERSIONS_ENDTIME    VERSIONS_XID      V      EMPNO
----------------- --------------------------------------------------------------------------- --------------- --------------------------------------------------------------------------- ---------------- - ----------
          2170077 21-JAN-21 05.30.10 PM                                                                                           0400130058040000 U        7902
                                                                                                      2170077 21-JAN-21 05.30.10 PM                                                                                 7902
                                                                                                                                                            7788
                                                                                                                                                           7698

--查询事务id

select start_scn,commit_scn,logon_user,operation,table_name,undo_sql
from flashback_transaction_query
where xid=hextoraw('0400130058040000');

--查出undo sql

SQL> update "SYS"."XYC_T1" set "SAL" = '9999' where ROWID = 'AAAVpSAABAAAW9hAAA';

1 row updated.

SQL> select * from xyc_t1;
select * from xyc_t1;

     EMPNO ENAME                JOB                       MGR HIREDATE                   SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ------------------- ---------- ---------- ----------
      7698 BLAKE                MANAGER                  7839 1981-05-01 00:00:00       2850                    30
      7902 FORD                 ANALYST                    7566 1981-12-03 00:00:00       9999                    20

--返回到上一版本,即sal值为9999

Related Posts