|
1. 闪回单点查询(9i就能做的闪回,闪回技术2,因为有undo所以能够见到提交以前的样子):
- SQL> conn hr/oracle_4U
- Connected.
- SQL> select salary from employees where employee_id=100;
- SALARY
- ----------
- 24000
- SQL> update employees set salary=25000 where employee_id=100;
- 1 row updated.
- SQL> commit;
- Commit complete.
- SQL> select salary from employees where employee_id=100;
- SALARY
- ----------
- 25000
- SQL> select salary from employees as of timestamp systimestamp-1/1440 where employee_id=100;
- SALARY
- ----------
- 24000
- SQL>
复制代码
2. 闪回技术1(10g, FLASHBACK_TRANSACTION_QUERY,是所有闪回的原理,查询它就是以Oracle的语言来查看整个UNDO表空间):
3. 闪回技术4 (闪回表):
- SQL> flashback table employees to scn 1165416;
- flashback table employees to scn 1165416
- *
- ERROR at line 1:
- ORA-08189: cannot flashback the table because row movement is not enabled
复制代码- SQL> alter table employees enable row movement;
- Table altered.
- SQL> flashback table employees to scn 1165416;
- Flashback complete.
- SQL> alter table employees disable row movement;
- Table altered.
- SQL> select salary from employees where employee_id=100;
- SALARY
- ----------
- 24000
- SQL>
复制代码
证明它是一个事务,而且还是删除再添加:
- select * from Flashback_Transaction_Query ftq
- where ftq.logon_user='HR' and ftq.table_name='EMPLOYEES'
- order by ftq.commit_timestamp;
-
复制代码
4. 闪回技术3(闪回版本查询,把 Flashback_Transaction_Query 的信息映射到自己的表(employees)的查询技术):
- SQL> select versions_xid, versions_startscn, versions_operation, salary from employees
- 2 versions between scn minvalue and maxvalue
- 3 where employee_id=100;
- VERSIONS_XID VERSIONS_STARTSCN V SALARY
- ---------------- ----------------- - ----------
- 05001400BB030000 1170658 I 24000
- 05001400BB030000 1170658 D 25000
- 25000
复制代码
versions_startscn就是flashback_transaction_query中的提交的时间。在EM上看同样versions查询,startscn被图形界面-1:
- oracle@station87 ~]$ sqlplus /nolog
- SQL*Plus: Release 11.2.0.1.0 Production on Wed Jun 6 04:18:23 2018
- Copyright (c) 1982, 2009, Oracle. All rights reserved.
- SQL> conn hr/oracle_4U
- Connected.
- SQL> select salary from employees where employee_id=100;
- SALARY
- ----------
- 24000
- SQL> update employees set salary=30000 where employee_id=100;
- 1 row updated.
- SQL> commit;
- Commit complete.
- SQL> update employees set salary=40000 where employee_id=100;
- 1 row updated.
- SQL> commit;
- Commit complete.
- SQL> update employees set salary=50000 where employee_id=100;
- 1 row updated.
- SQL> commit;
- Commit complete.
- SQL> select versions_xid, versions_startscn, versions_operation, salary from employees
- 2 versions between scn minvalue and maxvalue
- 3 where employee_id=100 order by versions_startscn;
- VERSIONS_XID VERSIONS_STARTSCN V SALARY
- ---------------- ----------------- - ----------
- 05001400BB030000 1170658 D 25000
- 05001400BB030000 1170658 I 24000
- 06001600A0030000 1172047 U 30000
- 02000E008D030000 1172182 U 40000
- 09000400C1030000 1172237 U 50000
- 25000
- 6 rows selected.
- SQL> /
- VERSIONS_XID VERSIONS_STARTSCN V SALARY
- ---------------- ----------------- - ----------
- 05001400BB030000 1170658 D 25000
- 05001400BB030000 1170658 I 24000
- 06001600A0030000 1172047 U 30000
- 02000E008D030000 1172182 U 40000
- 09000400C1030000 1172237 U 50000
- 09001600C7030000 1178141 D 50000
- 09001600C7030000 1178141 I 30000
- 25000
- 8 rows selected.
- SQL> select salary from employees where employee_id=100;
- SALARY
- ----------
- 30000
- SQL> clear
- SQL>
- SQL>
- SQL>
- SQL>
- SQL>
- SQL>
- SQL> !clear
- SQL> create table t05310_a( a number ) ;
- Table created.
- SQL> insert into t05310_a values (1) ;
- 1 row created.
- SQL> commit;
- Commit complete.
- SQL> update t05310_a set a=2 ;
- 1 row updated.
- SQL> commit;
- Commit complete.
- SQL> select versions_xid, versions_startscn, versions_operation,a from t05210_a
- 2
- SQL>
- SQL>
- SQL>
- SQL> select versions_xid, versions_startscn, versions_operation,a from t05310_a
- 2 versions between scn minvalue and maxvalue;
- VERSIONS_XID VERSIONS_STARTSCN V A
- ---------------- ----------------- - ----------
- 03000D00C6030000 1179717 U 2
- 0200140096030000 1179700 I 1
- SQL> select a from t05310_a as of scn 1179700 ;
- A
- ----------
- 1
- SQL> alter table t05310_a add ( b varchar2(20)) ;
- Table altered.
- SQL> update t05310_a set a=3,b='C' ;
- 1 row updated.
- SQL> commit;
- Commit complete.
- SQL> select * from t05310_a;
- A B
- ---------- --------------------
- 3 C
- SQL> select versions_xid, versions_startscn, versions_operation,a from t05310_a
- 2 versions between scn minvalue and maxvalue;
- VERSIONS_XID VERSIONS_STARTSCN V A
- ---------------- ----------------- - ----------
- 09001D00CA030000 1180048 U 3
- 03000D00C6030000 1179717 U 2
- 0200140096030000 1179700 I 1
- SQL> select versions_xid, versions_startscn, versions_operation,a,b from t05310_a
- 2 versions between scn minvalue and maxvalue;
- VERSIONS_XID VERSIONS_STARTSCN V A B
- ---------------- ----------------- - ---------- --------------------
- 09001D00CA030000 1180048 U 3 C
- 03000D00C6030000 1179717 U 2
- 0200140096030000 1179700 I 1
- SQL> select a from t05310_a as of scn 1179700 ;
- A
- ----------
- 1
- SQL> alter table t05310_a enable row movement;
- Table altered.
- SQL> flashback table t05310_a to scn 1179700;
- Flashback complete.
- SQL> select a from t05310_a ;
- A
- ----------
- 1
- SQL> select versions_xid, versions_startscn, versions_operation,a,b from t05310_a
- 2 versions between scn minvalue and maxvalue;versions between scn minvalue and maxvalue;
- versions between scn minvalue and maxvalue;versions between scn minvalue and maxvalue
- *
- ERROR at line 2:
- ORA-00911: invalid character
- SQL> select versions_xid, versions_startscn, versions_operation,a,b from t05310_a
- 2 versions between scn minvalue and maxvalue;
- VERSIONS_XID VERSIONS_STARTSCN V A B
- ---------------- ----------------- - ---------- --------------------
- 01000D00DA020000 1180198 I 1
- 01000D00DA020000 1180198 D 3 C
- 09001D00CA030000 1180048 U 3 C
- 03000D00C6030000 1179717 U 2
- 0200140096030000 1179700 I 1
- SQL> select a from t05310_a as of scn 1180198 ;
- A
- ----------
- 1
- SQL> update t05310_a set a=4 ;
- 1 row updated.
- SQL> COMMIT;
- Commit complete.
- SQL> select * from t05310_a;
- A B
- ---------- --------------------
- 4
- SQL> select versions_xid, versions_startscn, versions_operation,a,b from t05310_a
- 2 versions between scn minvalue and maxvalue;
- VERSIONS_XID VERSIONS_STARTSCN V A B
- ---------------- ----------------- - ---------- --------------------
- 06002100AB030000 1180318 U 4
- 01000D00DA020000 1180198 I 1
- 01000D00DA020000 1180198 D 3 C
- 09001D00CA030000 1180048 U 3 C
- 03000D00C6030000 1179717 U 2
- 0200140096030000 1179700 I 1
- 6 rows selected.
- SQL> select a from t05310_a as of scn 1180198 ;
- A
- ----------
- 1
- SQL> alter table t05310_a drop ( b) ;
- Table altered.
- SQL> select a from t05310_a as of scn 1180198 ;
- select a from t05310_a as of scn 1180198
- *
- ERROR at line 1:
- ORA-01466: unable to read data - table definition has changed
- SQL>
复制代码
5. 闪回技术8:闪回事务
- undefine s1
- create table &&s1( id number ) ;
- ! sleep 5
- insert into &&s1 values (1) ;
- insert into &&s1 values (2) ;
- insert into &&s1 values (3) ;
- commit;
- update &&s1 set id=11 where id=1;
- update &&s1 set id=22 where id=2;
- update &&s1 set id=33 where id=3;
- commit;
- update &&s1 set id=222 where id=22;
- update &&s1 set id=333 where id=33;
- commit;
- update &&s1 set id=22 where id=222;
- commit;
复制代码- select d.SUPPLEMENTAL_LOG_DATA_MIN,
- d.SUPPLEMENTAL_LOG_DATA_PK,
- d.SUPPLEMENTAL_LOG_DATA_FK
- from v_$database d;
- alter database add supplemental log data;
- alter database add supplemental log data (primary key) columns;
复制代码
----------------------------------------------------------------------------
T_NOCASCADE:
- select * from flashback_transaction_query ftq
- where ftq.logon_user='HR' and ftq.table_name='T_NOCASCADE'
- order by commit_Scn;
-
- begin
- dbms_flashback.transaction_backout(numtxns => 1,
- xids => xid_array('03000600D6030000'),
- options => dbms_flashback.nocascade);
- end;
复制代码
- SQL> select * from t_nocascade;
- ID
- ----------
- 11
- 22
- 333
复制代码
T_CASCADE:
- select * from flashback_transaction_query ftq
- where ftq.logon_user='HR' and ftq.table_name='T_CASCADE'
- order by commit_Scn;
- begin
- dbms_flashback.transaction_backout(numtxns => 1,
- xids => xid_array('0A000D002F030000'),
- options => dbms_flashback.cascade);
- end;
-
- select * from dba_flashback_txn_report;
-
- commit;
复制代码- <?xml version="1.0" encoding="ISO-8859-1"?>
- <COMP_XID_REPORT XID="04001B0021030000">
- <TRANSACTION XID="0A000D002F030000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="yes">
- update "HR"."T_CASCADE" set "ID" = '3' where "ID" = '33' and ROWID = 'AAATGVAAEAAAAI9AAC'
- </USQL>
- <USQL exec="yes">
- update "HR"."T_CASCADE" set "ID" = '2' where "ID" = '22' and ROWID = 'AAATGVAAEAAAAI9AAB'
- </USQL>
- <USQL exec="yes">
- update "HR"."T_CASCADE" set "ID" = '1' where "ID" = '11' and ROWID = 'AAATGVAAEAAAAI9AAA'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- <TRANSACTION XID="0400160020030000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="yes">
- update "HR"."T_CASCADE" set "ID" = '33' where "ID" = '333' and ROWID = 'AAATGVAAEAAAAI9AAC'
- </USQL>
- <USQL exec="yes">
- update "HR"."T_CASCADE" set "ID" = '22' where "ID" = '222' and ROWID = 'AAATGVAAEAAAAI9AAB'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- <TRANSACTION XID="01001E00E9020000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="yes">
- update "HR"."T_CASCADE" set "ID" = '222' where "ID" = '22' and ROWID = 'AAATGVAAEAAAAI9AAB'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- <EXECUTED_UNDO_SQL>
- <EXEC_USQL>update "HR"."T_CASCADE" set "ID" = '222' where "ID" = '22' and ROWID = 'AAATGVAAEAAAAI9AAB'
- </EXEC_USQL>
- <EXEC_USQL>update "HR"."T_CASCADE" set "ID" = '33' where "ID" = '333' and ROWID = 'AAATGVAAEAAAAI9AAC'
- </EXEC_USQL>
- <EXEC_USQL>update "HR"."T_CASCADE" set "ID" = '22' where "ID" = '222' and ROWID = 'AAATGVAAEAAAAI9AAB'
- </EXEC_USQL>
- <EXEC_USQL>update "HR"."T_CASCADE" set "ID" = '3' where "ID" = '33' and ROWID = 'AAATGVAAEAAAAI9AAC'
- </EXEC_USQL>
- <EXEC_USQL>update "HR"."T_CASCADE" set "ID" = '2' where "ID" = '22' and ROWID = 'AAATGVAAEAAAAI9AAB'
- </EXEC_USQL>
- <EXEC_USQL>update "HR"."T_CASCADE" set "ID" = '1' where "ID" = '11' and ROWID = 'AAATGVAAEAAAAI9AAA'
- </EXEC_USQL>
- </EXECUTED_UNDO_SQL>
- </COMP_XID_REPORT>
复制代码- SQL> select * from t_cascade;
- ID
- ----------
- 1
- 2
- 3
复制代码
----------------------------------------------------
T_NONCONFLICT_ONLY:
- select * from flashback_transaction_query ftq
- where ftq.logon_user='HR' and ftq.table_name='T_NONCONFLICT_ONLY'
- order by commit_Scn;
- begin
- dbms_flashback.transaction_backout(numtxns => 1,
- xids => xid_array('070015002A030000'),
- options => dbms_flashback.nonconflict_only);
- end;
- select * from dba_flashback_txn_report;
- commit;
复制代码
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <COMP_XID_REPORT XID="0800040094040000" NAME="_SYS_COMP_TXN_458772_TIM_1528234613">
- <TRANSACTION XID="070015002A030000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="no">
- update "HR"."T_NONCONFLICT_ONLY" set "ID" = '3' where "ID" = '33' and ROWID = 'AAATGWAAEAAAAJFAAC'
- </USQL>
- <USQL exec="no">
- update "HR"."T_NONCONFLICT_ONLY" set "ID" = '2' where "ID" = '22' and ROWID = 'AAATGWAAEAAAAJFAAB'
- </USQL>
- <USQL exec="yes">
- update "HR"."T_NONCONFLICT_ONLY" set "ID" = '1' where "ID" = '11' and ROWID = 'AAATGWAAEAAAAJFAAA'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- <TRANSACTION XID="0800030094040000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="no">
- update "HR"."T_NONCONFLICT_ONLY" set "ID" = '33' where "ID" = '333' and ROWID = 'AAATGWAAEAAAAJFAAC'
- </USQL>
- <USQL exec="no">
- update "HR"."T_NONCONFLICT_ONLY" set "ID" = '22' where "ID" = '222' and ROWID = 'AAATGWAAEAAAAJFAAB'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- <TRANSACTION XID="03001000D9030000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="no">
- update "HR"."T_NONCONFLICT_ONLY" set "ID" = '222' where "ID" = '22' and ROWID = 'AAATGWAAEAAAAJFAAB'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- <EXECUTED_UNDO_SQL>
- <EXEC_USQL>update "HR"."T_NONCONFLICT_ONLY" set "ID" = '1' where "ID" = '11' and ROWID = 'AAATGWAAEAAAAJFAAA'
- </EXEC_USQL>
- </EXECUTED_UNDO_SQL>
- </COMP_XID_REPORT>
复制代码- SQL> select * from t_nonconflict_only;
- ID
- ----------
- 1
- 22
- 333
复制代码
----------------------------------------------
t_nocascade_force:
- select * from flashback_transaction_query ftq
- where ftq.logon_user='HR' and ftq.table_name='T_NOCASCADE_FORCE'
- order by commit_Scn;
- begin
- dbms_flashback.transaction_backout(numtxns => 1,
- xids => xid_array('09000800E2030000'),
- options => dbms_flashback.nocascade_force);
- end;
- select * from dba_flashback_txn_report;
- commit;
复制代码- <?xml version="1.0" encoding="ISO-8859-1"?>
- <COMP_XID_REPORT XID="02001500AF030000" NAME="_SYS_COMP_TXN_458772_TIM_1528235102">
- <TRANSACTION XID="09000800E2030000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="yes">
- update "HR"."T_NOCASCADE_FORCE" set "ID" = '3' where "ID" = '33' and ROWID = 'AAATGXAAEAAAAJNAAC'
- </USQL>
- <USQL exec="yes">
- update "HR"."T_NOCASCADE_FORCE" set "ID" = '2' where "ID" = '22' and ROWID = 'AAATGXAAEAAAAJNAAB'
- </USQL>
- <USQL exec="yes">
- update "HR"."T_NOCASCADE_FORCE" set "ID" = '1' where "ID" = '11' and ROWID = 'AAATGXAAEAAAAJNAAA'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- <TRANSACTION XID="0400150025030000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="no">
- update "HR"."T_NOCASCADE_FORCE" set "ID" = '33' where "ID" = '333' and ROWID = 'AAATGXAAEAAAAJNAAC'
- </USQL>
- <USQL exec="no">
- update "HR"."T_NOCASCADE_FORCE" set "ID" = '22' where "ID" = '222' and ROWID = 'AAATGXAAEAAAAJNAAB'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- <TRANSACTION XID="03001A00DF030000">
- <CHARACTERISTICS>
- </CHARACTERISTICS>
- <UNDO_SQL>
- <USQL exec="no">
- update "HR"."T_NOCASCADE_FORCE" set "ID" = '222' where "ID" = '22' and ROWID = 'AAATGXAAEAAAAJNAAB'
- </USQL>
- </UNDO_SQL>
- <DEPENDENT_XIDS>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- </DEPENDENT_XIDS>
- </TRANSACTION>
- <EXECUTED_UNDO_SQL>
- <EXEC_USQL>update "HR"."T_NOCASCADE_FORCE" set "ID" = '3' where "ID" = '33' and ROWID = 'AAATGXAAEAAAAJNAAC'
- </EXEC_USQL>
- <EXEC_USQL>update "HR"."T_NOCASCADE_FORCE" set "ID" = '2' where "ID" = '22' and ROWID = 'AAATGXAAEAAAAJNAAB'
- </EXEC_USQL>
- <EXEC_USQL>update "HR"."T_NOCASCADE_FORCE" set "ID" = '1' where "ID" = '11' and ROWID = 'AAATGXAAEAAAAJNAAA'
- </EXEC_USQL>
- </EXECUTED_UNDO_SQL>
- </COMP_XID_REPORT>
复制代码- SQL> select * from t_nocascade_force ;
- ID
- ----------
- 1
- 2
- 333
复制代码
|
|