oralce恢复误删除的表中的数据(闪回、闪回查询)

 

今天,有个朋友和我说,他有个同事误操作将oracle中一个表误删除了,
一开始我以为是drop掉了,于是建议闪回表,但是回收站里找不到;
后来才知道是delete了所有数据,于是建议回滚或者用闪回查询的办法将数据找回来,
但是遇到快照太旧的问题,信息被覆盖了,只能找回部分记录了;
得知有备份,于是建议用备份在测试机上尝试恢复;

下面做个简单的实验:


SQL> desc test
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------

A VARCHAR2(10)
C NUMBER
D CHAR(10)

SQL> select * from test;

A C D
---------- ---------- ----------
def 2
dd 2 ff
defg 1

SQL> delete from test;

已删除3行。

SQL> select * from test;

未选定行

SQL> rollback;

SQL> select * from test;

A C D
---------- ---------- ----------
def 2
dd 2 ff
defg 1

SQL> delete from test;

已删除3行。

SQL> commit;

提交完成。

SQL> select * from test;

未选定行

SQL> conn zxh/zxh
已连接。
SQL> flashback table test to before drop rename to test;
flashback table test to before drop rename to test
*
1 行出现错误:
ORA-38305:
对象不在回收站中

SQL> conn / as sysdba
已连接。
SQL> show parameter recyclebin

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
recyclebin string on

SQL> select current_scn from v$database;

CURRENT_SCN
-----------
3472744

SQL> select * from zxh.test;

未选定行

SQL> select * from zxh.test as of scn 3472700;

未选定行

SQL> select * from zxh.test as of scn 3472600;

A C D
---------- ---------- ----------
def 2
dd 2 ff
defg 1

SQL> insert into zxh.test select * from zxh.test as of scn 3472600;

已创建3行。

SQL> commit;

提交完成。

SQL> select * from zxh.test;

A C D
---------- ---------- ----------
def 2
dd 2 ff
defg 1

SQL> conn zxh/zxh
已连接。
SQL> select * from test;

A C D
---------- ---------- ----------
def 2
dd 2 ff
defg 1

SQL> drop table test;

表已删除。

SQL> commit;

提交完成。


SQL> flashback table test to before drop rename to test;

闪回完成。

SQL> commit;

提交完成。

SQL> select * from test;

A C D
---------- ---------- ----------
def 2
dd 2 ff
defg 1

SQL>

恢复成功。

总结:
误删除表
如果是drop,这个命令是DDL语句,删除的信息保存在回收站(前提是recyclebin设置为on)中,这样可以从回收站中将表闪回;
如果是delete,这个命令是DML语句,删除的信息保存在undo中,可以用闪回查询的办法将数据恢复,多次尝试查找scn闪回查询,尽可能的恢复到执行误操作的前一个点(如果回滚段太小,有可能碰到经典的快照太旧问题)。

请使用浏览器的分享功能分享到微信等