恢复删除的数据:
oracle恢复删除的数据分为两种方法:scn和时间戳两种方法恢复。
一、通过scn恢复删除且已提交的数据
1、获得当前数据库的scn号 (切换到sys用户或system用户查询)
select current_scn from v$database;
或
select dbms_flashback.get_system_change_number from dual;
查询到的scn号为:122699735
2、查询误删数据的表在当前scn号之前的scn是否有已删除的数据
select * from 表名 as of scn 122699720; (确定删除的数据是否存在,如果存在,则恢复数据;如果不是,则继续缩小scn号)
3、恢复删除且已提交的数据
flashback table 表名 to scn 122699720;
二、通过时间戳恢复删除且已提交的数据
1、查询当前系统时间
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
或
select sysdate from dual;
2、查询误删数据的表在当前时间之前的时间点是否有已删除的数据
select * from 表名 as of timestamp to_timestamp('2016-09-21 15:29:00','yyyy-mm-dd hh24:mi:ss'); (如果不是,则继续缩小范围)
3、恢复删除且已提交的数据
flashback table 表名 to timestamp to_timestamp('2016-09-21 15:29:00','yyyy-mm-dd hh24:mi:ss');
注意:执行上面的语句可能会报错:
报错error: ORA-08189: cannot flashback the table because row movement is not enabled.(ORA-08189: 因为未启用行移动功能, 不能闪回表。)
解决办法:alter table table_name enable row movement;
再执行闪回语句,操作完成。
恢复删除的表
drop后的表被放在回收站(user_recyclebin)里,而不是直接删除掉。这样,回收站里的表信息就可以被恢复
通过查询回收站user_recyclebin获取被删除的表信息:
select * from user_recyclebin 查询当前用户回收站里面的内容
然后使用语句
flashback table <user_recyclebin.object_name or user_recyclebin.original_name> to before drop [rename to <new_table_name>];
将回收站里的表恢复为原名称或指定新名称,表中数据不会丢失。
例:
SQL> drop table emp_temp001;
Table dropped
SQL> select table_name from user_tables;
TABLE_NAME
------------------------------
DEPT
EMP
BONUS
SALGRADE
SYS_TEMP_FBT
SQL> flashback table EMP_TEMP001 to before drop rename to emp_temp;
Done
SQL> select table_name from user_tables;
TABLE_NAME
------------------------------
DEPT
EMP
BONUS
SALGRADE
SYS_TEMP_FBT
EMP_TEMP
6 rows selected
注:必须9i或10g以上版本支持,flashback无法恢复全文索引
若要彻底删除表,则使用语句:drop table <table_name> purge;
清除回收站里的信息
清除指定表:purge table <table_name>;
清除当前用户的回收站:purge recyclebin;
清除所有用户的回收站:purge dba_recyclebin;
不放入回收站,直接删除则是:drop table xx purge;