Oracle误删除表数据后的恢复详解
测试环境: SYSTEM:IBM AIX 5L Oracle Version:10gR2
1. undo_retention参数的查询与修改 使用show parameter undo命令查看当前的数据库参数undo_retention设置。 显示如下: SQL> show parameter undo NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ undo_management string AUTO undo_retention integer 900 undo_tablespace string UNDOTBS2 undo_retention(保持力),900单位是秒,即15分钟。 修改默认的undo_retention参数设置: SQL> ALTER SYSTEM SET undo_retention=10800 SCOPE=BOTH; System altered. SQL> show parameter undo NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ undo_management string AUTO undo_retention integer 10800 undo_tablespace string UNDOTBS2 undo_retention 10800,单位秒,即3小时。 2. oracle误删除表数据后的的快速恢复功能方法 2.1 方法一 set serveroutput on 2.2 方法二 查看FIRST_CHANGE#,NEXT_CHANGE#,FIRST_TIME SQL> set pagesize 9999 SQL> col fscn for 999999999 SQL> col nscn for 999999999 SQL> select name,FIRST_CHANGE# fscn,NEXT_CHANGE# nscn,FIRST_TIME from v$archived_log; 当前的SCN为: SQL> select dbms_flashback.get_system_change_number fscn from dual; FSCN ---------- 3435958
SQL> connect username/password Connected.
SQL> select count(*) from hs_passport; COUNT(*) ---------- 851998 创建恢复表: SQL> create table hs_passport_recov as select * from hs_passport where 1=0; Table created.
SQL> select count(*) from hs_passport as of scn 12929970422; COUNT(*) ---------- 861686
SQL> select count(*) from hs_passport as of scn &scn; Enter value for scn: 12929941968 old 1: select count(*) from hs_passport as of scn &scn new 1: select count(*) from hs_passport as of scn 12929941968 COUNT(*) ---------- 861684 SQL> / Enter value for scn: 12927633776 old 1: select count(*) from hs_passport as of scn &scn new 1: select count(*) from hs_passport as of scn 12927633776 select count(*) from hs_passport as of scn 12927633776 * ERROR at line 1: ORA-01466: unable to read data - table definition has changed SQL> / Enter value for scn: 12929928784 old 1: select count(*) from hs_passport as of scn &scn new 1: select count(*) from hs_passport as of scn 12929928784 COUNT(*) ---------- 825110 SQL> / Enter value for scn: 12928000000 old 1: select count(*) from hs_passport as of scn &scn new 1: select count(*) from hs_passport as of scn 12928000000 select count(*) from hs_passport as of scn 12928000000 * ERROR at line 1: ORA-01466: unable to read data - table definition has changed
SQL> insert into hs_passport_recov select * from hs_passport as of scn 12929941968; 861684 rows created. SQL> commit; Commit complete. 数据恢复简单例子 3. 下面我们通过一个例子来具体说明闪回查询的用法 示例 这个时间可以随便设,它表示在系统中保留提交了的UNDO信息的时间,10800就是保留3小时,即180分钟。 Connected. SQL> drop user lsf cascade; User dropped. SQL> create user lsf identified by lsf; User created. SQL> grant connect,resource to lsf; Grant succeeded. SQL> grant execute on dbms_flashback to lsf; Grant succeeded. SQL> conn lsf/lsf Connected. SQL> create table T(id int, name varchar2(20)); Table created. SQL> insert into T values(1,'lsf'); 1 row created. SQL> insert into T values(2,'lsf'); 1 row created. SQL> insert into T values(3,'lsf'); 1 row created. SQL> commit; Commit complete. SQL> select * from T; ID NAME ---------- ------------------------------------------------------------ 1 lsf 2 lsf 3 lsf SQL> set time on 10:12:50 SQL> delete from T where id=1; 1 row deleted. 10:13:02 SQL> commit; Commit complete. 10:13:10 SQL> select * from T; ID NAME ---------- ------------------------------------------------------------ 2 lsf 3 lsf 10:13:18 SQL> execute DBMS_FLASHBACK.ENABLE_AT_TIME(to_date('2011-04-15 10:12:50','YYYY-MM-DD HH24:MI:SS')); PL/SQL procedure successfully completed. 10:13:50 SQL> select * from T; ID NAME ---------- ------------------------------------------------------------ 1 lsf & |