在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Oracle误删除表数据后的恢复详解 1. undo_retention参数的查询与修改 使用show parameter undo命令查看当前的数据库参数undo_retention设置。 NAME TYPE VALUE System altered. SQL> show parameter undo NAME TYPE VALUE 2. oracle误删除表数据后的的快速恢复功能方法 2.1 方法一 exec dbms_flashback.enable_at_time(to_date('2011-04-15 08:21:00','yyyy-mm-dd hh24:mi:ss')); set serveroutput on DECLARE r_temp hr.job_history%ROWTYPE; CURSOR c_temp IS SELECT * FROM hr.job_history; BEGIN OPEN c_temp; dbms_flashback.disable; LOOP FETCH c_temp INTO r_temp; EXIT WHEN c_temp%NOTFOUND; insert into hr.job_history(EMPLOYEE_ID,JOB_ID,START_DATE,END_DATE) values (r_temp.EMPLOYEE_ID,r_temp.JOB_ID,r_temp.START_DATE,r_temp.END_DATE); commit; END LOOP; CLOSE c_temp; END; 这种办法可以将删除的数据恢复到对应的表中,首先要保证该用户有执行dbms_flashback包的权限。 2.2 方法二 查看FIRST_CHANGE#,NEXT_CHANGE#,FIRST_TIME 当前的SCN为: 使用应用用户尝试闪回 现有数据: Table created. 选择SCN向前恢复: 尝试多个SCN,获取最佳值(如果能得知具体时间,那么可以获得准确的数据闪回) 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 最后选择恢复到SCN为12929941968的时间点 861684 rows created. SQL> commit; Commit complete. 数据恢复简单例子 3. 下面我们通过一个例子来具体说明闪回查询的用法 示例 SQL> conn /as sysdba 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 2 lsf 3 lsf 10:13:57 SQL> execute DBMS_FLASHBACK.DISABLE; PL/SQL procedure successfully completed. 10:15:48 SQL> select * from T; ID NAME ---------- ------------------------------------------------------------ 2 lsf 3 lsf 3.3 使用闪回查询恢复数据 10:16:59 SQL> truncate table T; Table truncated. 10:18:15 SQL> select * from T; no rows selected 10:18:22 SQL> insert into T values(1,'lsf'); 1 row created. 10:19:42 SQL> insert into T values(2,'lsf'); 1 row created. 10:19:48 SQL> insert into T values(3,'lsf'); 1 row created. 10:19:55 SQL> insert into T values(4,'lsf'); 1 row created. 10:20:07 SQL> insert into T values(5,'lsf'); 1 row created. 10:20:15 SQL> insert into T values(6,'lsf'); 1 row created. 10:20:21 SQL> commit; Commit complete. 10:20:26 SQL> select * from T; ID NAME ---------- ------------------------------------------------------------ 1 lsf 2 lsf 3 lsf 4 lsf 5 lsf 6 lsf 6 rows selected. 10:20:56 SQL> delete T; 6 rows deleted. 10:21:27 SQL> commit; Commit complete. 10:21:40 SQL> declare 10:22:29 2 cursor flash_recover is 10:22:43 3 select * from T; 10:22:50 4 t_recode T%rowtype; 10:23:11 5 begin 10:23:14 6 DBMS_FLASHBACK.ENABLE_AT_TIME(to_date('2011-04-15 10:20:56','YYYY-MM-DD HH24:MI:SS')); 10:24:22 7 open flash_recover; 10:24:39 8 DBMS_FLASHBACK.DISABLE; 10:24:59 9 loop 10:25:05 10 FETCH flash_recover into t_recode; 10:25:24 11 EXIT WHEN flash_recover%NOTFOUND; 10:25:45 12 insert into T values(t_recode.id,t_recode.name); 10:26:35 13 end loop; 10:26:39 14 CLOSE FLASH_RECOVER; 10:26:50 15 commit; 10:26:56 16 end; 10:26:58 17 / PL/SQL procedure successfully completed. 10:27:00 SQL> select * from T; ID NAME ---------- ------------------------------------------------------------ 1 lsf 2 lsf 3 lsf 4 lsf 5 lsf 6 lsf 6 rows selected. 我们可以已经恢复了所有的6条纪录,但是由于闪回查询的局限性,有可能不能恢复所有的6条记录,原因就在下面。 4. 局限性 10:27:27 SQL> VARIABLE SCN_SAVE NUMBER; 10:32:47 SQL> EXECUTE :SCN_SAVE := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER; PL/SQL procedure successfully completed. 10:33:24 SQL> print SCN_SAVE; SCN_SAVE ---------- 3438420 10:33:41 SQL> execute DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(:SCN_SAVE); PL/SQL procedure successfully completed. 10:34:31 SQL> select * from T; ID NAME ---------- ------------------------------------------------------------ 1 lsf 2 lsf 3 lsf 4 lsf 5 lsf 6 lsf 6 rows selected. 另外,在使用DBMS_FLASHBACK.ENABLE_AT_TIME前,你必须设定你的NLS_DATE_FORMAT的精确程度,Oracle默认的是精确到天,如果你不设定,像上面的例子你不会得到预期结果。 |
请发表评论