Oracle的flashback特性之二:Flashback Table

一、从recyclebin中恢复

flashback table tablename to before drop;

tablename 可以是表名也可以是recyclebin中的对象表,支持多表同时操作,表名之间以逗号分隔。

 1、从recyclebin恢复一个被删除的表。

(1)

SQL> select object_name,original_name from recyclebin;

OBJECT_NAME
------------------------------------------------------------
ORIGINAL_NAME
----------------------------------------------------------------
BIN$zQXVBB0C/4TgQKjACv18Aw==$0
TEST1

(2)

SQL> flashback table test1 to before drop;

Flashback complete.

(3)

SQL> select object_name,original_name from recyclebin;

no rows selected

2、如果要恢复的表再当前schema中已存在同名的表,可以在闪回恢复时通过rename to 指定一个新的表名。 (1)模拟场景:

 SQL> create table test2 as select * from test1;

Table created.

SQL> drop table test1;

Table dropped.

SQL> alter table test2 rename to test1;

Table altered.


SQL> flashback table test1 to before drop;
flashback table test1 to before drop
*
ERROR at line 1:
ORA-38312: original name is used by an existing object

(2)闪回table时应用rename to

 SQL> flashback table test1 to before drop rename to new_test1;

Flashback complete.

二、从undo中恢复

 如果表不是被删掉,而是反复修改多次,希望恢复到某个时间点,flashback query可以做,但要较多的where条件。

flashback table tablename to scn/timestamp

(1)记录当前scn

SQL> select current_scn from v$database;

CURRENT_SCN
-----------
     518475
(2)提交一些事务。
 SQL> select * from test1;
        ID NAME
---------- --------------------
         6 ff
         7 gg
         6 dd
         7 ee

SQL> update test1 set id = id + 1 where id > 6;

2 rows updated.


SQL> delete from test1 where id = 6 and name = 'ff';

1 row deleted.

SQL> insert into test1 values(1, 'aa');

1 row created.

SQL> commit;

Commit complete.

(3)发现上述事务提交是误操作,需要回撤。

SQL> flashback table test1 to scn 518475;
flashback table test1 to scn 518475
                *
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled

这是由于基于undo的表恢复,被恢复表的必须启用row movement

 SQL>alter table test1 enable row movement;

Table altered.

SQL> select row_movement from user_tables where table_name = 'TEST1';

ROW_MOVEMENT
----------------
ENABLED

禁用alter table test1 disable row movement;

执行闪回:

SQL> flashback table test1 to scn 518475;

Flashback complete.
SQL> select * from test1;

        ID NAME
---------- --------------------
         6 ff
         7 gg
         6 dd
         7 ee
原文地址:https://www.cnblogs.com/guarder/p/3472164.html