数据库中闪回有哪些
小编给大家分享一下数据库中闪回有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
闪回主要有闪回表、闪回查询、闪回数据库(数据来自闪回日志)三种,这里主要了解闪回表和闪回查询
闪回表:又分以下2种:
Flashback table tablename to before drop(数据来自回收站)
Flashback table tablename to scn/timestamp(数据来自UNDO,必须开启Row movement)
闪回查询:又分以下3种
闪回查询AS OF SCN/TIMESTAMP(数据来自UNDO)
闪回版本查询Flashback Version Query(数据来自UNDO)
闪回事务查询Flashback Transaction Query(数据来自UNDO)
闪回表中关于Row movement和Recyclebin的知识点
The row_movement_clause lets you specify whether the database can move a table row. Specify ENABLE to allow the database to move a row, thus changing the rowid.
Row movement must be enabled for all tables in the Flashback list unless you are flashing back the table TO BEFORE DROP. That operation is called a flashback drop operation, and it uses dropped data in the recycle bin rather than undo data.
row_movement_clause允许您指定数据库是否可以移动表行。指定ENABLE以允许数据库移动一行,从而更改rowid。
闪回必须开启行移动,但是闪回DROP不需要开启Row movement,且数据来自回收站
Flashback table后不需要提交,直接就自动提交了
Flashback table user_tables.TABLE_NAME to before drop(user_tables.TABLE_NAME就是recyclebin.ORIGINAL_NAME)
Flashback table recyclebin.OBJECT_NAME to before drop(当一个table删除两次时,可以指定回收站里名称按顺序指定回收哪次删除的表)
Flashback table TABLE_NAME to before drop rename to TABLE_NAME2(当删除一个表后,又建立了一张同名表时,可以使用rename to恢复成其他名称的表)
Purge recyclebin只是清空当前用户的回收站
Purge dba_recyclebin清空所有用户的回收站
闪回表、闪回查询的一个示例
SQL> create table hr.test1(id number primary key,name varchar2(10));
Table created.
SQL> begin
2 insert into test1 values(1,'A');
3 insert into test1 values(2,'B');
4 insert into test1 values(3,'C');
5 commit;
6 dbms_lock.sleep(5);
7 update test1 set name='C-1' where id=3;
8 insert into test1 values(4,'D');
9 commit;
10 end;
11 /
PL/SQL procedure successfully completed.
--闪回版本查询
SQL> select versions_startscn,versions_endscn,versions_xid,versions_operation,name from test1 versions BETWEEN SCN MINVALUE AND MAXVALUE;
VERSIONS_STARTSCN VERSIONS_ENDSCN VERSIONS_XID VE NAME
----------------- --------------- ---------------- -- --------------------
4083242 03002000610D0000 I D
4083242 03002000610D0000 U C-1
4083235 4083242 01000400970C0000 I C
4083235 01000400970C0000 I B
4083235 01000400970C0000 I A
--闪回查询AS OF SCN
SQL> select * from test1 as of scn 4083235;
ID NAME
---------- --------------------
1 A
2 B
3 C
--闪回版本查询
SQL> select versions_startscn,versions_endscn,versions_xid,versions_operation,name from test1 versions BETWEEN TIMESTAMP TO_TIMESTAMP('2017-08-16 14:17:10','YYYY-MM-DD HH24:MI:SS') AND TO_TIMESTAMP('2017-08-16 14:19:26','YYYY-MM-DD HH24:MI:SS');
VERSIONS_STARTSCN VERSIONS_ENDSCN VERSIONS_XID VE NAME
----------------- --------------- ---------------- -- --------------------
4083242 03002000610D0000 I D
4083242 03002000610D0000 U C-1
4083235 4083242 01000400970C0000 I C
4083235 01000400970C0000 I B
4083235 01000400970C0000 I A
--闪回查询AS OF TIMESTAMP
SQL> select * from test1 AS OF TIMESTAMP TO_TIMESTAMP('2017-08-16 14:17:16','YYYY-MM-DD HH24:MI:SS');
ID NAME
---------- --------------------
1 A
2 B
3 C
--闪回事务查询
SQL> SELECT undo_sql FROM flashback_transaction_query WHERE xid = HEXTORAW('03002000610D0000');
UNDO_SQL
--------------------------------------------------------------------------------
delete from "HR"."TEST1" where ROWID = 'AAAWV2AAEAAAAJXAAD';
update "HR"."TEST1" set "NAME" = 'C' where ROWID = 'AAAWV2AAEAAAAJXAAC';
SQL> select * from test1;
ID NAME
---------- --------------------
1 A
2 B
3 C-1
4 D
--启用ROW MOVEMENT
SQL> ALTER TABLE test1 ENABLE ROW MOVEMENT;
Table altered.
--Flashback table to SCN
SQL> flashback table test1 to scn 4083235;
Flashback complete.
SQL> select * from test1;
ID NAME
---------- --------------------
1 A
2 B
3 C
SQL> drop table test1;
Table dropped.
--Flashback table to BEFORE DROP
SQL> FLASHBACK TABLE test1 TO BEFORE DROP;
Flashback complete.
SQL> drop table test1;
Table dropped.
--Flashback table to BEFORE DROP RENAME
SQL> FLASHBACK TABLE test1 TO BEFORE DROP RENAME TO test1_2017;
Flashback complete.
SQL> select * from test1_2017;
ID NAME
---------- --------------------
1 A
2 B
3 C
以上是“数据库中闪回有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!