数据库

img llmmysun

oracle 闪回查询 实例

发表于2004/11/2 11:34:00  2994人阅读

 
一、简介                                                                      

  在过去,如果用户误删
们必须求助DBA来对数据库
中提供了一项新的技术手段
可以针对错误进行相应的恢
/更新了数据后,作为用户并没
进行恢复,到了Oracle9i,这一
--闪回查询,用户使用闪回查询
复措施,而这一切都无需DBA干
有什么直接的方法来进行恢复,他
个难堪局面有所改善。Oracle 9i
可以及时取得误操作前的数据,并
预。

  下面我们通过一个例子来具体说明闪回查询的用法。                                

  二、示例                                                                      

  1、使用闪回查询前必须确定下面两个参数:                                      

  UNDO_MANAGEMENT = AUTO                                 

  undo_retention = 120
的时间,1200就是保留20分
0; #这个时间可以随便设,他表
钟。
示在系统中保留提交了的UNDO信息


  2、使用闪回查询                                                              

  SQL> conn sys/sys as sysdba


  Connected.                                                          

  SQL> create user flash identified by flash;


  User created.                                                    

  SQL> grant connect, resource to flash;


  Grant succeeded.                                              

  SQL> grant execute on dbms_flashback to flash;


  Grant succeeded.                                              

  SQL> conn flash/flash


  Connected.                                                          

  SQL> set echo on


  SQL> create table t (a varchar2(10));


  Table created.                                                  

  SQL> insert into t values('gototop');


  1 row created.                                                  

  SQL> insert into t values('www.ncn.cn');


  1 row created.                                                  

  SQL> /


  SQL> select * from t;


  A                                                                            

  ----------                                                          

  gototop                                                                

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  SQL> set time on


  15:00:22 SQL>


  15:00:22 SQL>


  15:00:23 SQL> delete t where a='gototop';


  1 row deleted.                                                  

  15:00:35 SQL> commit;


  Commit complete.                                              

  15:00:38 SQL> select * from t;


  A                                                                            

  ----------                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  15:08:22 SQL> execute DBMS_FLASHBACK.ENABLE_AT_TIME(to_date('2003-05-16 15:00:00'));



  PL/SQL procedure suc
cessfully completed.

  15:09:02 SQL> select * from t;


  A                                                                            

  ----------                                                          

  gototop                                                                

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  15:16:43 SQL> execute DBMS_FLASHBACK.DISABLE;


  PL/SQL procedure successfully co
mpleted.

  15:17:05 SQL> select * from t;


  A                                                                            

  ----------                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  3、使用闪回查询恢复数据:                                                    

  15:36:55 SQL> select * from t;


  no rows selected                                              

  15:36:57 SQL> insert into t values('www.ncn.cn');


  1 row created.                                                  

  15:37:15 SQL> /


  1 row created.                                                  

  15:37:16 SQL> /


  1 row created.                                                  

  15:37:17 SQL> /


  1 row created.                                                  

  15:37:18 SQL> /


  1 row created.                                                  

  15:37:19 SQL> commit;


  Commit complete.                                              

  15:37:21 SQL> /


  Commit complete.                                              

  15:37:22 SQL> /


  Commit complete.                                              

  15:37:23 SQL> select * from t;


  A                                                                            

  ----------                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  6 rows selected.                                              

  15:37:28 SQL>


  15:38:44 SQL> delete t;


  6 rows deleted.                                                

  15:38:50 SQL> commit;


  Commit complete.                                              

  15:38:53 SQL> commit;


  Commit complete.                                              

  15:38:54 SQL> /


  Commit complete.                                              

  15:38:57 SQL> declare


  15:39:28 2 cursor flash_recover is          

  15:39:28 3 select * from t;                        

  15:39:28 4 t_recode t%rowtype;                  

  15:39:28 5 begin                                              

  15:39:28 6 DBMS_FLASHBACK.ENABLE
_AT_TIME(to_date('2003-05-16 15:37:23'));

  15:39:28 7 open FLASH_RECOVER;                  

  15:39:28 8 DBMS_FLASHBACK.DISABLE;          

  15:39:28 9 loop                                                


  15:39:28 10 FETCH FL
ASH_RECOVER INTO t_recode;

  15:39:28 11 EXIT WHEN FLASH_RECO
VER%NOTFOUND;


  15:39:28 12 insert i
nto t values (t_recode.a);

  15:39:28 13 end loop;                                    

  15:39:28 14 CLOSE FLASH_RECOVER;              

  15:39:28 15 commit;                                        

  15:39:28 16 end;                                              

  15:39:28 17 /                                                    


  PL/SQL procedure suc
cessfully completed.

  15:39:28 SQL> 15:39:28 SQL>


  15:39:31 SQL> select * from t;


  A                                                                            

  ----------                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  www.ncn.cn                                                          

  15:39:35 SQL>


  我们可以已经恢复了5
下面。
条纪录,但我们要恢复的6条纪

录,为什么会少一条呢?原因就在


  三、局限性                                                                    

  1、闪回查询是基于SCN的,虽然我执行的是:                                  

  DBMS_FLASHBACK.ENABLE_AT_TIME(to
_date('2003-05-16 15:37:23'));

  但Oracle并不会精确的这个时间点,
始进行恢复。而Oracle 9i是每五分钟记
而是ROUND DOWN到最近的一次SCN,然后从这个SCN开
录一次SCN的,并将SCN和对应时间的映射做个纪录。

  这正是上面我们进行恢
CK.ENABLE_AT_TIME来进行
复时少了一条的原因。因此如果
恢复,为了避免恢复失败,我可
使用DBMS_FLASHBA
以先等5分钟,然后再进行恢复。

  使用DBMS_FLASHBACK.E
和对应时间的映射信息只会
复5天前的数据。如果你想
SCN,然后使用
NABLE_AT_TIME进行恢复还有一
保留5天,因此我们无法通过DBM
使用闪回查询来恢复5天前的数

个缺点,那就是在Oracle 9i中SCN
S_FLASHBACK.ENABLE_AT_TIME来恢
据,你必须自己来确定需要恢复的


  DBMS_FLASHBACK.ENABLE_AT_SYSTEM_
点,下面是使用方法:
CHANGE_NUMBER(SCN_NUMBER); 来定位你的恢复时间


  15:58:58 SQL> VARIABLE SCN_SAVE NUMBER;


  16:13:43 SQL> EXECUTE :SCN_SAVE := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER;



  PL/SQL procedure suc
cessfully completed.

  16:13:50 SQL> print SCN_SAVE;


  SCN_SAVE                                                              

  ----------                                                          

  2.1202E+11                                                          

  16:28:34 SQL> execute DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(:SCN_SAVE);



  PL/SQL procedure suc
cessfully completed.

  另外,在使用DBMS_FLASHBACK.ENABL
确程度,Oracle默认的是精确到天,如果
E_AT_TIME前,你必须设定你的NLS_DATE_FORMAT的精
你不设定,像上面的例子你不会得到预期结果。

  2、如果你使用sysdate
SCN值,你必须注意它们取
和DBMS_FLASHBACK.GET_SYSTEM_
得都是当前的时间点和SCN值。
CHANGE_NUMBER来获取时间点或者


  3、你只能在事务开始时进入闪回查
询模式,如果之前有DML操作,则必须COMMIT。

  4、闪回查询无法恢复到表结构改变之前,因为闪回查询使用的当前的数据字典。      
阅读全文
0 0

相关文章推荐

img
取 消
img