要是说flashback query看成是恢复记录的话,那么flashback table就是用来恢复表的,oracle 10G以后新引入了recycle bin的功能,类似Windows回收站。
被删除的表,其实并非真正的删除,而是通过修改数据字典的方式将其改名并放入recycle bin中,如果要恢复recycle bin中的对象,借助flashback table 是最简单直接的方式。
除此之外,flashback table 也提供类似flashback query中的AS OF TIMESTAMP/SCN的方法,借助undo表空间中的undo数据,将现有的表恢复到指定时间点或者scn的状态。
---------- ----------------------
11 kk
3 aa
12:57:26 SQL> create index db1.mkind on db1.milktwo(id);
Index created.
12:58:24 SQL> purge recyclebin;
Recyclebin purged.
12:58:39 SQL> drop table db1.milktwo;
Table dropped.
12:59:01 SQL> desc dba_recyclebin;
Name Null? Type ----------------------------------------------------------------------------------------------
NOT NULL VARCHAR2(30) OBJECT_NAME
NOT NULL VARCHAR2(30) ORIGINAL_NAME
VARCHAR2(32) OPERATION
VARCHAR2(9) TYPE
VARCHAR2(25) TS_NAME
VARCHAR2(30) CREATETIME
VARCHAR2(19) DROPTIME
VARCHAR2(19) DROPSCN
NUMBER PARTITION_NAME
VARCHAR2(32) CAN_UNDROP
VARCHAR2(3) CAN_PURGE
VARCHAR2(3) RELATED
NOT NULL NUMBER BASE_OBJECT
NOT NULL NUMBER PURGE_OBJECT
NOT NULL NUMBER SPACE
NUMBER
12:59:30 SQL> select owner,OBJECT_NAME,ORIGINAL_NAME from dba_recyclebin; //当删除表的时候,对应的索引也会被放入recycle bin
OWNER OBJECT_NAME ORIGINAL_NAME
------------------------------ ------------------------------ --------------------------------
DB1 BIN$UtbPKaL3CHPgUwtYqMAwog==$0 MKIND
DB1 BIN$UtbPKaL4CHPgUwtYqMAwog==$0 MILKTWO
13:01:45 SQL> flashback table db1.milktwo to before drop; //也可以flashback table db1."BIN$UtbPKaL4CHPgUwtYqMAwog==$0" to before drop;
Flashback complete.
13:02:31 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
11 kk
3 aa
13:05:03 SQL> select owner,table_name,index_name,status from dba_indexes where table_name='MILKTWO'; //闪回以后对应的表的索引不会恢复到原来名称
OWNER TABLE_NAME INDEX_NAME STATUS
------------------------------ ------------------------------ ------------------------------ --------
DB1 MILKTWO BIN$UtbPKaL3CHPgUwtYqMAwog==$0 VALID
13:05:55 SQL> alter index db1."BIN$UtbPKaL3CHPgUwtYqMAwog==$0"rename to mkind; //通过rename重命名
Index altered.
13:06:09 SQL> select owner,table_name,index_name,status from dba_indexes where table_name='MILKTWO';
OWNER TABLE_NAME INDEX_NAME STATUS
------------------------------ ------------------------------ ------------------------------ --------
DB1 MILKTWO MKIND VALID
13:30:32 SQL> flashback table db1.milktwo to before drop rename to milktwo2; //当闪回之前已经有新表创建了,可以rename
Flashback complete.
//当同一名称的表先后被删除多次,那么flashback的时候如果不指定recyclebin中的OBJECT_NAME,那么 flashback的机制是先恢复最后删除的,详细验证过程如下:
13:40:22 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
11 kk
3 aa
13:41:07 SQL> drop table db1.milktwo;
Table dropped.
13:41:59 SQL> create table db1.milktwo (id number,name varchar2(22));
Table created.
13:42:15 SQL> insert into db1.milktwo values(33,'kk');
1 row created.
13:42:40 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
13:42:52 SQL> drop table db1.milktwo;
Table dropped.
13:43:09 SQL> desc dba_recyclebin
Name Null? Type -------------
NOT NULL VARCHAR2(30) OBJECT_NAME
NOT NULL VARCHAR2(30) ORIGINAL_NAME
VARCHAR2(32) OPERATION
VARCHAR2(9) TYPE
VARCHAR2(25) TS_NAME
VARCHAR2(30) CREATETIME
VARCHAR2(19) DROPTIME
VARCHAR2(19) DROPSCN
NUMBER PARTITION_NAME
VARCHAR2(32) CAN_UNDROP
VARCHAR2(3) CAN_PURGE
VARCHAR2(3) RELATED
NOT NULL NUMBER BASE_OBJECT
NOT NULL NUMBER PURGE_OBJECT
NOT NULL NUMBER SPACE
NUMBER
13:43:25 SQL> select OWNER,OBJECT_NAME,ORIGINAL_NAME,DROPTIME from dba_recyclebin where owner='DB1'; //查看recyclebin
OWNER OBJECT_NAME ORIGINAL_NAME DROPTIME
------------------------------ ------------------------------ -------------------------------- -------------------
DB1 BIN$UtbPKaL8CHPgUwtYqMAwog==$0 MILKTWO 2017-06-26:13:41:21
DB1 BIN$UtbPKaL9CHPgUwtYqMAwog==$0 MILKTWO 2017-06-26:13:43:09
13:45:10 SQL> flashback table db1.milktwo to before drop;
Flashback complete.
13:45:48 SQL> select * from db1.milktwo; //恢复了第二次删的表
ID NAME
---------- ----------------------
33 kk
基于undo的表恢复被恢复的表必须启用row movement,表的row movement的属性用来控制是否允许修改列值所造成的记录移动,默认值是 DISABLED
14:06:56 SQL> select row_movement from dba_tables where TABLE_NAME='MILKTWO'; //查看默认情况的row_movement值
ROW_MOVE
--------
DISABLED
14:08:13 SQL> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
3533043
14:08:46 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
14:09:03 SQL> insert into db1.milktwo values (5,'ll');
1 row created.
14:09:23 SQL> commit;
Commit complete.
14:09:28 SQL> update db1.milktwo set id=31 where name='kk';
1 row updated.
14:11:52 SQL> commit;
Commit complete.
14:11:55 SQL> flashback table db1.milktwo to scn 3533043; //row movement没有开的情况下,闪回失败
flashback table db1.milktwo to scn 3533043
*
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled
14:12:27 SQL> alter table db1.milktwo enable row movement; //enable row movement
Table altered.
14:13:12 SQL> flashback table db1.milktwo to scn 3533043; //闪回成功
Flashback complete.
//既然基于undo的,那么就要注意到ddl对flashback table as of timestamp/scn的影响
14:17:23 SQL> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
3533626
14:13:19 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
14:18:09 SQL> insert into db1.milktwo values (2,'hh');
1 row created.
14:18:35 SQL> commit;
Commit complete.
14:19:10 SQL> alter table db1.milktwo move; / /ddl操作
Table altered.
14:19:51 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
2 hh
14:20:00 SQL> flashback table db1.milktwo as of scn 3533626; //flashback 失败
flashback table db1.milktwo as of scn 3533626
*
ERROR at line 1:
ORA-08187: snapshot expression not allowed here
flashback table 基于undo的其他注意事项:
1.flashback table 实际上是dml操作,因此需要注意triggers对其的影响,flashback table to scn在执行的时候自动回disable掉相关的trigger,如果你想在此期间triggers继续发挥作用,可以在flashback table时附加enabletriggers子句。
2.基于undo的表恢复,索引会自动维护,但是统计信息并不会恢复到指定时间。
3.基于undo表的恢复也有局限性,不支持以下类型的表:聚簇表,物化视图,高级对列表,系统表,远程表,嵌套表及表分区或者子分区。
flashback table 基于recycle bin的其他注意事项:
1.flashback drop不能恢复参照完整性,就是说如果此表有主外键约束,那么恢复之后,该约束的disable的,需要手工处理
2.所操作的表必须存在于本地管理表空间
3.被恢复的表的关联对象并不会恢复到之前的名称,可以rename
4.当删除表的时候,依赖于此表的视图也会被删除,但是由于物化视图并不会放入recyclebin中,那么flashback的时候并不会恢复。
5.相对于被删除的表而言,当表空间不足的时候,oracle会先清理被删除的表索引,再是表,所以恢复出来可能没有索引
6.flashback drop 支持同时操作多个表,以逗号隔开
被删除的表,其实并非真正的删除,而是通过修改数据字典的方式将其改名并放入recycle bin中,如果要恢复recycle bin中的对象,借助flashback table 是最简单直接的方式。
除此之外,flashback table 也提供类似flashback query中的AS OF TIMESTAMP/SCN的方法,借助undo表空间中的undo数据,将现有的表恢复到指定时间点或者scn的状态。
12:57:12 SQL> select * from db1.milktwo;
ID NAME---------- ----------------------
11 kk
3 aa
12:57:26 SQL> create index db1.mkind on db1.milktwo(id);
Index created.
12:58:24 SQL> purge recyclebin;
Recyclebin purged.
12:58:39 SQL> drop table db1.milktwo;
Table dropped.
12:59:01 SQL> desc dba_recyclebin;
Name Null? Type ----------------------------------------------------------------------------------------------
NOT NULL VARCHAR2(30) OBJECT_NAME
NOT NULL VARCHAR2(30) ORIGINAL_NAME
VARCHAR2(32) OPERATION
VARCHAR2(9) TYPE
VARCHAR2(25) TS_NAME
VARCHAR2(30) CREATETIME
VARCHAR2(19) DROPTIME
VARCHAR2(19) DROPSCN
NUMBER PARTITION_NAME
VARCHAR2(32) CAN_UNDROP
VARCHAR2(3) CAN_PURGE
VARCHAR2(3) RELATED
NOT NULL NUMBER BASE_OBJECT
NOT NULL NUMBER PURGE_OBJECT
NOT NULL NUMBER SPACE
NUMBER
12:59:30 SQL> select owner,OBJECT_NAME,ORIGINAL_NAME from dba_recyclebin; //当删除表的时候,对应的索引也会被放入recycle bin
OWNER OBJECT_NAME ORIGINAL_NAME
------------------------------ ------------------------------ --------------------------------
DB1 BIN$UtbPKaL3CHPgUwtYqMAwog==$0 MKIND
DB1 BIN$UtbPKaL4CHPgUwtYqMAwog==$0 MILKTWO
13:01:45 SQL> flashback table db1.milktwo to before drop; //也可以flashback table db1."BIN$UtbPKaL4CHPgUwtYqMAwog==$0" to before drop;
Flashback complete.
13:02:31 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
11 kk
3 aa
13:05:03 SQL> select owner,table_name,index_name,status from dba_indexes where table_name='MILKTWO'; //闪回以后对应的表的索引不会恢复到原来名称
OWNER TABLE_NAME INDEX_NAME STATUS
------------------------------ ------------------------------ ------------------------------ --------
DB1 MILKTWO BIN$UtbPKaL3CHPgUwtYqMAwog==$0 VALID
13:05:55 SQL> alter index db1."BIN$UtbPKaL3CHPgUwtYqMAwog==$0"rename to mkind; //通过rename重命名
Index altered.
13:06:09 SQL> select owner,table_name,index_name,status from dba_indexes where table_name='MILKTWO';
OWNER TABLE_NAME INDEX_NAME STATUS
------------------------------ ------------------------------ ------------------------------ --------
DB1 MILKTWO MKIND VALID
13:30:32 SQL> flashback table db1.milktwo to before drop rename to milktwo2; //当闪回之前已经有新表创建了,可以rename
Flashback complete.
//当同一名称的表先后被删除多次,那么flashback的时候如果不指定recyclebin中的OBJECT_NAME,那么 flashback的机制是先恢复最后删除的,详细验证过程如下:
13:40:22 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
11 kk
3 aa
13:41:07 SQL> drop table db1.milktwo;
Table dropped.
13:41:59 SQL> create table db1.milktwo (id number,name varchar2(22));
Table created.
13:42:15 SQL> insert into db1.milktwo values(33,'kk');
1 row created.
13:42:40 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
13:42:52 SQL> drop table db1.milktwo;
Table dropped.
13:43:09 SQL> desc dba_recyclebin
Name Null? Type -------------
NOT NULL VARCHAR2(30) OBJECT_NAME
NOT NULL VARCHAR2(30) ORIGINAL_NAME
VARCHAR2(32) OPERATION
VARCHAR2(9) TYPE
VARCHAR2(25) TS_NAME
VARCHAR2(30) CREATETIME
VARCHAR2(19) DROPTIME
VARCHAR2(19) DROPSCN
NUMBER PARTITION_NAME
VARCHAR2(32) CAN_UNDROP
VARCHAR2(3) CAN_PURGE
VARCHAR2(3) RELATED
NOT NULL NUMBER BASE_OBJECT
NOT NULL NUMBER PURGE_OBJECT
NOT NULL NUMBER SPACE
NUMBER
13:43:25 SQL> select OWNER,OBJECT_NAME,ORIGINAL_NAME,DROPTIME from dba_recyclebin where owner='DB1'; //查看recyclebin
OWNER OBJECT_NAME ORIGINAL_NAME DROPTIME
------------------------------ ------------------------------ -------------------------------- -------------------
DB1 BIN$UtbPKaL8CHPgUwtYqMAwog==$0 MILKTWO 2017-06-26:13:41:21
DB1 BIN$UtbPKaL9CHPgUwtYqMAwog==$0 MILKTWO 2017-06-26:13:43:09
13:45:10 SQL> flashback table db1.milktwo to before drop;
Flashback complete.
13:45:48 SQL> select * from db1.milktwo; //恢复了第二次删的表
ID NAME
---------- ----------------------
33 kk
基于undo的表恢复被恢复的表必须启用row movement,表的row movement的属性用来控制是否允许修改列值所造成的记录移动,默认值是 DISABLED
14:06:56 SQL> select row_movement from dba_tables where TABLE_NAME='MILKTWO'; //查看默认情况的row_movement值
ROW_MOVE
--------
DISABLED
14:08:13 SQL> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
3533043
14:08:46 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
14:09:03 SQL> insert into db1.milktwo values (5,'ll');
1 row created.
14:09:23 SQL> commit;
Commit complete.
14:09:28 SQL> update db1.milktwo set id=31 where name='kk';
1 row updated.
14:11:52 SQL> commit;
Commit complete.
14:11:55 SQL> flashback table db1.milktwo to scn 3533043; //row movement没有开的情况下,闪回失败
flashback table db1.milktwo to scn 3533043
*
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled
14:12:27 SQL> alter table db1.milktwo enable row movement; //enable row movement
Table altered.
14:13:12 SQL> flashback table db1.milktwo to scn 3533043; //闪回成功
Flashback complete.
//既然基于undo的,那么就要注意到ddl对flashback table as of timestamp/scn的影响
14:17:23 SQL> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
3533626
14:13:19 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
14:18:09 SQL> insert into db1.milktwo values (2,'hh');
1 row created.
14:18:35 SQL> commit;
Commit complete.
14:19:10 SQL> alter table db1.milktwo move; / /ddl操作
Table altered.
14:19:51 SQL> select * from db1.milktwo;
ID NAME
---------- ----------------------
33 kk
2 hh
14:20:00 SQL> flashback table db1.milktwo as of scn 3533626; //flashback 失败
flashback table db1.milktwo as of scn 3533626
*
ERROR at line 1:
ORA-08187: snapshot expression not allowed here
flashback table 基于undo的其他注意事项:
1.flashback table 实际上是dml操作,因此需要注意triggers对其的影响,flashback table to scn在执行的时候自动回disable掉相关的trigger,如果你想在此期间triggers继续发挥作用,可以在flashback table时附加enabletriggers子句。
2.基于undo的表恢复,索引会自动维护,但是统计信息并不会恢复到指定时间。
3.基于undo表的恢复也有局限性,不支持以下类型的表:聚簇表,物化视图,高级对列表,系统表,远程表,嵌套表及表分区或者子分区。
flashback table 基于recycle bin的其他注意事项:
1.flashback drop不能恢复参照完整性,就是说如果此表有主外键约束,那么恢复之后,该约束的disable的,需要手工处理
2.所操作的表必须存在于本地管理表空间
3.被恢复的表的关联对象并不会恢复到之前的名称,可以rename
4.当删除表的时候,依赖于此表的视图也会被删除,但是由于物化视图并不会放入recyclebin中,那么flashback的时候并不会恢复。
5.相对于被删除的表而言,当表空间不足的时候,oracle会先清理被删除的表索引,再是表,所以恢复出来可能没有索引
6.flashback drop 支持同时操作多个表,以逗号隔开