很多情况下为了能快速找回被删除的表,可以利用回收站找到并重建被删除的表。
(一)
回收站里是否可以找到被删除的表,和undo的保留策略、空间大小有很大关系,超时或者空间不足都会导致无法闪回表,只能从备份中恢复。
# 查看当前undo 保留策略
SQL> show parameter undo_
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 3600
undo_tablespace string UNDOTBS1
(二)
# 测试表
SQL> select table_name from dba_tables where owner='WORKING';
TABLE_NAME
------------------------------
DBA_OBJECTS
###被删除的对象可以在回收站中查看,现在没有内容
SQL> select count(*) from dba_recyclebin;
COUNT(*)
----------
0
(三)
drop 表
# 删除数据表
SQL> drop table working.DBA_OBJECTS;
Table dropped.
# 回收站中保留了刚删除的表的信息,原对象 working.DBA_OBJECTS 在回收站中有一个映射名字 BIN$NSM8ZSPA8vvgU2W8CgpvQg==$0
SQL> select owner,object_name,original_name from dba_recyclebin;
OWNER OBJECT_NAME
------------------------------ ------------------------------
ORIGINAL_NAME
--------------------------------
WORKING BIN$NSM8ZSPA8vvgU2W8CgpvQg==$0
DBA_OBJECTS
(四)
利用闪回特性,还原被删除的数据表,并重命名
# 可以直接闪回表,也可以rename 表名
SQL> flashback table working.DBA_OBJECTS to before drop rename to DBA_OBJECTS_bak;
Flashback complete.
SQL> select table_name from dba_tables where owner='WORKING';
TABLE_NAME
------------------------------
DBA_OBJECTS_BAK
SQL> select count(*),owner from working.DBA_OBJECTS_bak group by owner;
COUNT(*) OWNER
---------- ------------------------------
1 WORKING
10 OUTLN
3340 PUBLIC
512 RPTADM
609 SYSTEM
8 ORACLE_OCM
124 GWADM
55 DBSNMP
5 APPQOSSYS
9592 SYS
124 PORTALADM
11 rows selected.
(五)
误更新数据的恢复(update delete)
如果知道确切的误操作时间,可以闪回至某个时间点,比如20分钟之前
# 拿上一步恢复的表做测试
SQL> select object_name,owner from working.DBA_OBJECTS_bak where owner='WORKING';
OBJECT_NAME
--------------------------------------------------------------------------------
OWNER
------------------------------
DBA_OBJECTS
WORKING
###修改数据
SQL> update working.DBA_OBJECTS_bak set object_name = 'DBA_OBJECTS_UPDATE' where owner = 'WORKING';
1 row updated.
SQL> commit;
Commit complete.
###第二次更新
SQL> update working.DBA_OBJECTS_bak set object_type = 'TABLE_UPDATE' where owner = 'WORKING';
1 row updated.
SQL> commit;
Commit complete.
###查看更新后的数据
SQL> select object_name,owner,object_type from working.DBA_OBJECTS_bak where owner='WORKING';
OBJECT_NAME
--------------------------------------------------------------------------------
OWNER OBJECT_TYPE
------------------------------ -------------------
DBA_OBJECTS_UPDATE
WORKING TABLE_UPDATE
(六)
闪回数据到20分钟之前
SQL> flashback table WORKING.DBA_OBJECTS_BAK to timestamp(systimestamp-interval '20' minute);
Flashback complete.
SQL> select object_name,owner,object_type from working.DBA_OBJECTS_bak where owner='WORKING';
OBJECT_NAME
--------------------------------------------------------------------------------
OWNER OBJECT_TYPE
------------------------------ -------------------
DBA_OBJECTS
WORKING TABLE
超出回滚段保留的时效,可以通过备份文件进行恢复需要的对象。