【Oracle】闪回表

时间:2021-06-28 15:16:08

语法:

FLASHBACK TABLE [ schema. ] table [, [ schema. ] table ]...

TO { { { SCN | TIMESTAMP } expr| RESTORE POINT restore_point}

[ { ENABLE | DISABLE } TRIGGERS ]| BEFORE DROP [ RENAME TO table ]} ;

注  flashback table  为 ddl

1.创建测试表T1,并插入数据

SCOTT@lgr> create table t1 (x number(2),d date);

Table created.

SCOTT@lgr> insert into t1 (x) values(1);

1 row created.

SCOTT@lgr> insert into t1 values(2,sysdate);

1 row created.

SCOTT@lgr> commit;

Commit complete.

SCOTT@lgr> select * from t1;

X D

---------- -------------------

1

2 2017-02-19,10:53:41

2.记录当前的时间

SCOTT@lgr> select sysdate from dual;

SYSDATE

-------------------

2017-02-19,10:54:49

3.删除T1表中的一条数据

SCOTT@lgr> delete t1 where x=2;

1 row deleted.

SCOTT@lgr> commit;

Commit complete.

SCOTT@lgr> select * from t1;

X D

---------- -------------------

1

4.此时对表进行闪回,会出现错误。原因很明显,因为表T1没有开启行移动

SCOTT@lgr> flashback table t1 to timestamp to_date('2017-02-19,10:54:49','yyyy-mm-dd,hh24:mi:ss');

flashback table t1 to timestamp to_date('2017-02-19,10:54:49','yyyy-mm-dd,hh24:mi:ss')

*

ERROR at line 1:

ORA-08189: cannot flashback the table because row movement is not enabled

SCOTT@lgr> select table_name,row_movement from user_tables where table_name='T1';

TABLE_NAME                     ROW_MOVE

------------------------------ --------

T1                             DISABLED

5.对表T1开启行移动

SCOTT@lgr> alter table t1 enable row movement;

Table altered.

6.对表T1进行基于时间点的闪回操作

SCOTT@lgr> flashback table t1 to timestamp to_date('2017-02-19,10:54:49','yyyy-mm-dd,hh24:mi:ss');

Flashback complete.

SCOTT@lgr> select * from t1;

X D

---------- -------------------

1

2 2017-02-19,10:53:41