如何利用Flashback Query 恢復誤刪除的數據

網上有不少關於數據回覆的文章,這裏整理一篇供你們參考,但願能幫助的你們!html

推薦一家即時通信雲服務商:www.yun2win.com,功能包含im即時通信實時音視頻、電子白板、屏幕共享的多種融合通信雲能力!數據庫

好了,言歸正傳!若是你在工做中誤刪除了一部分重要的數據,而且已經提交,若是你須要回覆這些數據,你能夠登錄到數據庫上仔細查看,是Oracle9iR2的話,你能夠首先嚐試使用flashback query閃回數據。oracle

 

◆第一步:確認數據庫的SCN變化:spa

 

  1. SQL> col fscn for 9999999999999999999
  2. SQL> col nscn for 9999999999999999999
  3. SQL> select name,FIRST_CHANGE# fscn,NEXT_CHANGE# nscn,FIRST_TIME from v$archived_log;
  4. ...................
  5. NAME                               FSCN       NSCN FIRST_TIME
  6. ----------------------------     ---------   -------------------
  7. /mwarch/oracle/1_52413.dbf     12929941968   12929942881 2005-06-22 14:38:28
  8. /mwarch/oracle/1_52414.dbf     12929942881   12929943706 2005-06-22 14:38:32
  9. /mwarch/oracle/1_52415.dbf     12929943706   12929944623 2005-06-22 14:38:35
  10. /mwarch/oracle/1_52416.dbf     12929944623   12929945392 2005-06-22 14:38:38
  11. /mwarch/oracle/1_52417.dbf     12929945392   12929945888 2005-06-22 14:38:41
  12. /mwarch/oracle/1_52418.dbf     12929945888   12929945965 2005-06-22 14:38:44
  13. /mwarch/oracle/1_52419.dbf     12929945965   12929948945 2005-06-22 14:38:45
  14. /mwarch/oracle/1_52420.dbf     12929948945   12929949904 2005-06-22 14:46:05
  15. /mwarch/oracle/1_52421.dbf     12929949904   12929950854 2005-06-22 14:46:08
  16. /mwarch/oracle/1_52422.dbf     12929950854   12929951751 2005-06-22 14:46:11
  17. /mwarch/oracle/1_52423.dbf     12929951751   12929952587 2005-06-22 14:46:14
  18. ...................
  19. /mwarch/oracle/1_52498.dbf     12930138975   12930139212 2005-06-22 15:55:57
  20. /mwarch/oracle/1_52499.dbf     12930139212   12930139446 2005-06-22 15:55:59
  21. /mwarch/oracle/1_52500.dbf     12930139446   12930139682 2005-06-22 15:56:00
  22. NAME                             FSCN           NSCN FIRST_TIME
  23. -------------------------  -------------       -----------
  24. /mwarch/oracle/1_52501.dbf     12930139682    12930139915 2005-06-22 15:56:02
  25. /mwarch/oracle/1_52502.dbf     12930139915    12930140149 2005-06-22 15:56:03
  26. /mwarch/oracle/1_52503.dbf     12930140149    12930140379 2005-06-22 15:56:05
  27. /mwarch/oracle/1_52504.dbf     12930140379    12930140610 2005-06-22 15:56:05
  28. /mwarch/oracle/1_52505.dbf     12930140610    12930140845 2005-06-22 15:56:07
  29. 14811 rows selected.


 

◆此時的SCN爲:code



  1. SQL> select dbms_flashback.
  2. get_system_change_number fscn from dual;
  3.                 FSCN
  4. --------------------
  5.          12930142214





◆此時使用應用用戶嘗試閃回視頻

 

  1. SQL> connect username/password
  2. Connected.

 

◆現有的數據htm

  1. SQL> select count(*) from hs_passport;
  2.  
  3.   COUNT(*)
  4. ----------
  5.     851998

 



◆建立恢復表get

  1. SQL> create table hs_passport_recov 
  2. as select * from hs_passport where 1=0;
  3. Table created.

 

◆此處選擇SCN向前恢復flash

  1.  
  2. 如下爲引用的內容:
  3. SQL> select count(*) from hs_passport as of scn 12929970422;
  4.   COUNT(*)
  5. ----------
  6.     861686

 

◆嘗試多個SCN,獲取最佳值(註釋:若是能夠得知具體時間,那樣則能夠得到準確的數據閃回)it

  1.  
  2. 如下爲引用的內容:
  3. SQL> select count(*) from hs_passport as of scn &scn;
  4. Enter value for scn: 12929941968
  5. old   1: select count(*) from hs_passport as of scn &scn
  6. new   1: select count(*) from hs_passport as of scn 12929941968
  7.   COUNT(*)
  8. ----------
  9.     861684
  10. SQL> /
  11. Enter value for scn: 12927633776
  12. old   1: select count(*) from hs_passport as of scn &scn
  13. new   1: select count(*) from hs_passport as of scn 12927633776
  14. select count(*) from hs_passport as of scn 12927633776
  15.                     *
  16. ERROR at line 1:
  17. ORA-01466: unable to read data - table definition has changed
  18. SQL> /
  19. Enter value for scn: 12929928784
  20. old   1: select count(*) from hs_passport as of scn &scn
  21. new   1: select count(*) from hs_passport as of scn 12929928784
  22.   COUNT(*)
  23. ----------
  24.     825110
  25. SQL> /
  26. Enter value for scn: 12928000000
  27. old   1: select count(*) from hs_passport as of scn &scn
  28. new   1: select count(*) from hs_passport as of scn 12928000000
  29. select count(*) from hs_passport as of scn 12928000000
  30.                      *
  31. ERROR at line 1:
  32. ORA-01466: unable to read data - table definition has changed

 

 

◆注意:最後選擇恢復到SCN爲12929941968的時間點

  1.  
  2. 如下爲引用的內容:
  3. SQL> 
  4. insert into hs_passport_recov 
  5. select * from hs_passport as of scn 12929941968;
  6. 861684 rows created.
  7. SQL> commit;
  8. Commit complete.

 

註釋:至此,則能夠知足須要,找回誤刪除的部分數據,閃回恢復成功。

相關文章
相關標籤/搜索