Oracle Flashback Transaction Query with Oracle Flashback Version Query
In this example, a database administrator does this:
DROP TABLE emp; CREATE TABLE emp ( empno NUMBER PRIMARY KEY, empname VARCHAR2(16), salary NUMBER ); INSERT INTO emp (empno, empname, salary) VALUES (111, 'Mike', 555); COMMIT; DROP TABLE dept; CREATE TABLE dept ( deptno NUMBER, deptname VARCHAR2(32) ); INSERT INTO dept (deptno, deptname) VALUES (10, 'Accounting'); COMMIT;
Now emp
and dept
have one row each. In terms of row versions, each table has one version of one row. Suppose that an erroneous transaction deletes empno
111
from table emp
:
UPDATE emp SET salary = salary + 100 WHERE empno = 111; INSERT INTO dept (deptno, deptname) VALUES (20, 'Finance'); DELETE FROM emp WHERE empno = 111; COMMIT;
Next, a transaction reinserts empno
111
into the emp
table with a new employee name:
INSERT INTO emp (empno, empname, salary) VALUES (111, 'Tom', 777); UPDATE emp SET salary = salary + 100 WHERE empno = 111; UPDATE emp SET salary = salary + 50 WHERE empno = 111; COMMIT;
The database administrator detects the application error and must diagnose the problem. The database administrator issues this query to retrieve versions of the rows in the emp
table that correspond to empno 111
. The query uses Oracle Flashback Version Query pseudocolumns:
SELECT versions_xid XID, versions_startscn START_SCN, versions_endscn END_SCN, versions_operation OPERATION, empname, salary FROM emp VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE WHERE empno = 111;
Results are similar to:
XID START_SCN END_SCN O EMPNAME SALARY ---------------- ---------- ---------- - ---------------- ---------- 09001100B2200000 10093466 I Tom 927 030002002B210000 10093459 D Mike 555 0800120096200000 10093375 10093459 I Mike 555 3 rows selected.
The results table rows are in descending chronological order. The third row corresponds to the version of the row in the table emp
that was inserted in the table when the table was created. The second row corresponds to the row in emp
that the erroneous transaction deleted. The first row corresponds to the version of the row in emp
that was reinserted with a new employee name.
The database administrator identifies transaction 030002002B210000
as the erroneous transaction and uses Oracle Flashback Transaction Query to audit all changes made by this transaction:
SELECT xid, start_scn, commit_scn, operation, logon_user, undo_sql
FROM flashback_transaction_query
WHERE xid = HEXTORAW('000200030000002D');
Results are similar to:
XID START_SCN COMMIT_SCN OPERATION LOGON_USER ---------------- ---------- ---------- --------- ------------------------------ UNDO_SQL -------------------------------------------------------------------------------- 030002002B210000 10093452 10093459 DELETE HR insert into "HR"."EMP"("EMPNO","EMPNAME","SALARY") values ('111','Mike','655'); 030002002B210000 10093452 10093459 INSERT HR delete from "HR"."DEPT" where ROWID = 'AAATjuAAEAAAAJrAAB'; 030002002B210000 10093452 10093459 UPDATE HR update "HR"."EMP" set "SALARY" = '555' where ROWID = 'AAATjsAAEAAAAJ7AAA'; 030002002B210000 10093452 10093459 BEGIN HR 4 rows selected.
To make the result of the next query easier to read, the database administrator uses these SQL*Plus commands:
COLUMN operation FORMAT A9 COLUMN table_name FORMAT A10 COLUMN table_owner FORMAT A11
To see the details of the erroneous transaction and all subsequent transactions, the database administrator performs this query:
SELECT xid, start_scn, commit_scn, operation, table_name, table_owner FROM flashback_transaction_query WHERE table_owner = 'HR' AND start_timestamp >= TO_TIMESTAMP ('2002-04-16 11:00:00','YYYY-MM-DD HH:MI:SS');
Results are similar to:
XID START_SCN COMMIT_SCN OPERATION TABLE_NAME TABLE_OWNER ---------------- ---------- ---------- --------- ---------- ----------- 02000E0074200000 10093435 10093446 INSERT DEPT HR 030002002B210000 10093452 10093459 DELETE EMP HR 030002002B210000 10093452 10093459 INSERT DEPT HR 030002002B210000 10093452 10093459 UPDATE EMP HR 0800120096200000 10093374 10093375 INSERT EMP HR 09001100B2200000 10093462 10093466 UPDATE EMP HR 09001100B2200000 10093462 10093466 UPDATE EMP HR 09001100B2200000 10093462 10093466 INSERT EMP HR 8 rows selected.