Oracle从回收站恢复删除的表
Oracle 10g以后的版本中引入了”回收站”(Recycle Bin)的概念,删除的对象统一放入回收站,以BIN$最为前缀命名.用户删除某个表,可以很容易的从”回收站”中还原回来,但在9i以前是对于删除表这样的DDL操纵是不能直接还原回来的,通常需要做不完全恢复或是使用EXP/IMP来恢复.
1.查看当前回收站功能是否开启(默认情况下是开启的)
SQL> column value format a10;
SQL> select value from V$parameter where name = ’recyclebin’;
1.查看当前回收站功能是否开启(默认情况下是开启的)
SQL> column value format a10;
SQL> select value from V$parameter where name = ’recyclebin’;
VALUE
———-
On
可以使用如下命令开启或关闭该功能.
SQL> alter system set recyclebin = on scope=spfile;
或 SQL> alter session set recyclebin = on scope=spfile;
SQL> alter system set recyclebin = off scope=spfile; 或 SQL> alter session set recyclebin = off scope=spfile;
2.查看回收站里的内容
SQL> show recyclebin;
或 SQL> select * from user_recyclebin; 下面将创建的表删除后放入回收站
SQL> show user; user is ”HXL”
SQL> create table test_rbin(val number);
———-
On
可以使用如下命令开启或关闭该功能.
SQL> alter system set recyclebin = on scope=spfile;
或 SQL> alter session set recyclebin = on scope=spfile;
SQL> alter system set recyclebin = off scope=spfile; 或 SQL> alter session set recyclebin = off scope=spfile;
2.查看回收站里的内容
SQL> show recyclebin;
或 SQL> select * from user_recyclebin; 下面将创建的表删除后放入回收站
SQL> show user; user is ”HXL”
SQL> create table test_rbin(val number);
Table created.
SQL> insert into test_rbin(val) values(10);
SQL> insert into test_rbin(val) values(10);
1 row created.
SQL> commit;
Commit complete.
SQL> drop table test_rbin; Table dropped.
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
—————- ———————————————– ——– ——–
TEST_RBIN BIN$S4u3UNw0QQ6t6LRxMMz7hQ==$0 TABLE 2012-06-22:15:01:30
Commit complete.
SQL> drop table test_rbin; Table dropped.
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
—————- ———————————————– ——– ——–
TEST_RBIN BIN$S4u3UNw0QQ6t6LRxMMz7hQ==$0 TABLE 2012-06-22:15:01:30
3.从回收站恢复删除的表
可以使用如下语句从回收站恢复删除的表
flashback table table_name to before drop;
flashback table table_name to before drop;
例如恢复刚才删的表
SQL> flashback table test_rbin to before drop;
SQL> flashback table test_rbin to before drop;
Flashback complete.
SQL> select * from test_rbin;
SQL> select * from test_rbin;
VALUE
———-
10
在恢复的过程的同时我们可以将表另外命名,命令如下:
flashback table << dropped table name >> to before drop rename to <<new table name >>
———-
10
在恢复的过程的同时我们可以将表另外命名,命令如下:
flashback table << dropped table name >> to before drop rename to <<new table name >>
SQL>flashback table test_rbin to before drop rename to test_rbin1;
Oracle从回收站恢复是按照”降序”恢复的,比如连续3次删除同样一个表(删除后再创建,再删除),恢复的是先恢复最后一次删除的表.
SQL> show user; USER is ”HXL”
SQL> drop table test_rbin;
SQL> show user; USER is ”HXL”
SQL> drop table test_rbin;
Table dropped.
SQL> create table test_rbin (col1 number);
SQL> create table test_rbin (col1 number);
Table created.
SQL> insert into test_rbin values (1);
SQL> insert into test_rbin values (1);
1 row created.
SQL> commit;
Commit complete.
SQL> drop table test_rbin;
Commit complete.
SQL> drop table test_rbin;
Table dropped.
SQL> create table test_rbin (col1 number);
SQL> create table test_rbin (col1 number);
Table created.
SQL> insert into test_rbin values (2);
SQL> insert into test_rbin values (2);
1 row created.
SQL> commit;
Commit complete.
SQL> drop table test_rbin;
Commit complete.
SQL> drop table test_rbin;
Table dropped.
SQL> create table test_rbin (col1 number);
SQL> create table test_rbin (col1 number);
Table created.
SQL> insert into test_rbin values (3);
SQL> insert into test_rbin values (3);
1 row created.
SQL> commit;
Commit complete.
SQL> drop table test_rbin;
Commit complete.
SQL> drop table test_rbin;
Table dropped.
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
—————- ———————————————– ——– ——————-
TEST_RBIN BIN$gGHkV/xQSSaQ5bG0PikSAg==$0 TABLE 2012-06-22:15:31:21
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
—————- ———————————————– ——– ——————-
TEST_RBIN BIN$gGHkV/xQSSaQ5bG0PikSAg==$0 TABLE 2012-06-22:15:31:21
TEST_RBIN BIN$xVKygKwJTJa+8zmu4BJzvQ==$0 TABLE 2012-06-22:15:30:53
TEST_RBIN BIN$R7As9PsYRva7CY6cnAjROw==$0 TABLE 2012-06-22:15:30:23
TEST_RBIN BIN$0R+cRKhDTFu+8ShBjLDpqg==$0 TABLE 2012-06-22:15:29:27
SQL> select * from test_rbin1; COL1
———-
3
SQL> select * from test_rbin2; COL1
———-
2
SQL> select * from test_rbin3; COL1
———-
1
4.清空回收站
清空某个表,清空表的同时也将该表上的索引和约束都一起清空了的.
SQL> purge table << table_name >> 清空索引,保留表数据
SQL> purge index <<index_name >>; 清空表空间
SQL> purge tablespace <<table space name>>;
清空某个用户下的表空间
SQL> purge tablespace <<table space name>> user <<user name>>; 清空整个回收站
SQL> purge recyclebin;
删除表的同时从回收站中清除
SQL> drop table << Table_Name >> purge;
说明:
若表上建有索引,在恢复表后,索引也一起恢复了且不会失效,但索引名字不是之前的名字了,而是以BIN$前缀的名字,这个时候按需要可以命名该索引(alter index ”BIN$XXXXX” rename to original_name,注意回收站里的名称需要双引号);
表以及表上的索引和约束,删除会进入到回收站,其他对象如存储过程,包,函数删除后,在回收站是找不到的,可以说回收站只收集删除后的表.
一旦表从回收站清除后,就不能使用FLASHBACK恢复了;
SQL> select * from test_rbin1; COL1
———-
3
SQL> select * from test_rbin2; COL1
———-
2
SQL> select * from test_rbin3; COL1
———-
1
4.清空回收站
清空某个表,清空表的同时也将该表上的索引和约束都一起清空了的.
SQL> purge table << table_name >> 清空索引,保留表数据
SQL> purge index <<index_name >>; 清空表空间
SQL> purge tablespace <<table space name>>;
清空某个用户下的表空间
SQL> purge tablespace <<table space name>> user <<user name>>; 清空整个回收站
SQL> purge recyclebin;
删除表的同时从回收站中清除
SQL> drop table << Table_Name >> purge;
说明:
若表上建有索引,在恢复表后,索引也一起恢复了且不会失效,但索引名字不是之前的名字了,而是以BIN$前缀的名字,这个时候按需要可以命名该索引(alter index ”BIN$XXXXX” rename to original_name,注意回收站里的名称需要双引号);
表以及表上的索引和约束,删除会进入到回收站,其他对象如存储过程,包,函数删除后,在回收站是找不到的,可以说回收站只收集删除后的表.
一旦表从回收站清除后,就不能使用FLASHBACK恢复了;