自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Oracle檢索數(shù)據(jù)一致性與事務(wù)恢復(fù)

數(shù)據(jù)庫 Oracle 數(shù)據(jù)庫運維
本文介紹在Oracle中檢索數(shù)據(jù)一致性的方法,以及用戶想要回滾數(shù)據(jù)(做事務(wù)恢復(fù))的兩種情況以及處理方法。Oracle為了保證用戶檢索數(shù)據(jù)的一致性,通過UNDO記錄,當(dāng)用戶檢索數(shù)據(jù)庫數(shù)據(jù)時,Oracle總是使用戶只能看到被提交過的數(shù)據(jù)或特定時間點的數(shù)據(jù)。

Oracle為了保證用戶檢索數(shù)據(jù)的一致性, 通過UNDO記錄,當(dāng)用戶檢索數(shù)據(jù)庫數(shù)據(jù)時,Oracle總是使用戶只能看到被提交過的數(shù)據(jù)或特定時間點的數(shù)據(jù)(select語句時間點),UNDO記錄會被存放到回滾段中,假如該數(shù)據(jù)未提交,用戶檢索數(shù)據(jù)時,都是從UNDO記錄中取得的.(如下圖:)

從UNDO記錄中取得 

1. ORACLE檢索數(shù)據(jù)一致性

先打開一個SecureCRT.(第一個session)

先建一個表

  1. SQL> create table c(a int);  
  2. Table created.  
  3. SQL> alter table c add b number;  
  4. Table altered.  
  5. SQL> desc c   
  6.  Name                                      Null?    Type  
  7.  ----------------------------------------- -------- --------------------------------------------  
  8.  A                                                  NUMBER(38)  
  9.  B                                                  NUMBER  

表中插入數(shù)據(jù)并提交

  1. SQL> insert into c values(1,2);  
  2. 1 row created.  
  3. SQL> insert into c values(3,4);  
  4. 1 row created.  
  5. SQL> select * from c;  
  6.          A          B  
  7. ---------- -----------------------------  
  8.          1          2  
  9.          3          4  
  10. SQL> commit;  
  11. Commit complete.  

再打開一個SecureCRT.(第二個session)

查詢

  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- --------------------------  
  4.          1          2  
  5.          3          4  

第一個session更改表中的數(shù)據(jù)但不提交

  1. SQL> update c set b=10 where a=1;  
  2. 1 row updated.  

第二個session查詢(修改但沒有提交檢索的是UNDO中的數(shù)據(jù))

  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- --------------------------  
  4.          1          2  
  5.          3          4  

第一個session提交

  1. SQL> commit;  
  2. Commit complete.  

第二個會話查詢(可見只有提交后才能檢索到數(shù)據(jù)段的數(shù)據(jù))

  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- -------------------------  
  4.          1         10  
  5.          3          4  

結(jié)論:如果用戶修改數(shù)據(jù)但沒有提交,其它用戶檢索的都是UNDO段的數(shù)據(jù),這樣就保證了數(shù)據(jù)的一致性

2.回滾數(shù)據(jù)(事務(wù)恢復(fù))

1.當(dāng)用戶updata數(shù)據(jù)但還沒有提交

  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- -----------------------------  
  4.          1          10  
  5.          3          4  
  6. SQL> update c set b=2 where a=1;  
  7. SQL> select * from c;  
  8.          A          B  
  9. ---------- -----------------------------  
  10.          1          2  
  11.          3          4  

這時用戶突然后悔了,想恢復(fù)到原來的狀態(tài)

  1. SQL> rollback;  
  2. Rollback complete.  
  3. SQL> commit;  
  4.    
  5. SQL> select * from c;  
  6.          A          B  
  7. ---------- -----------------------  
  8.          1         10  
  9.          3          4  

可見當(dāng)用戶用命今rollback還能回滾到初始狀態(tài).
 
2.當(dāng)用戶updata數(shù)據(jù)且已提交

當(dāng)用戶updata數(shù)據(jù)且已提交后,可以根據(jù)SCN記錄把數(shù)據(jù)還源.

先查看原始數(shù)據(jù)

  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- ----------  
  4.          1         10  
  5.          3          4  

找到SCN

  1. SQL> select current_scn from v$database;  
  2. CURRENT_SCN  
  3. -----------  
  4.      693636  

現(xiàn)在刪除表中的數(shù)據(jù)并提交

  1. SQL> delete from c;  
  2. rows deleted.  
  3. SQL> commit;         
  4. Commit complete.  

查詢(現(xiàn)在表中已沒有數(shù)據(jù)了)

  1. SQL> select * from c;  
  2. no rows selected  

檢索特定SCN的數(shù)據(jù)

  1. SQL> select * from c as of scn 693636;  
  2.          A          B  
  3. ---------- ----------  
  4.          1         10  
  5.          3          4  

恢復(fù)數(shù)據(jù)

  1. SQL> insert into c select * from c as of scn 693636;  
  2. rows created.  
  3. SQL> commit;  
  4. Commit complete.  

現(xiàn)在再查詢

  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- ----------------------  
  4.          1         10  
  5.          3          4  

可見可以根據(jù)SCN恢復(fù)到某一檢查點的數(shù)據(jù),如果把SCN轉(zhuǎn)換成時間,,就可以把數(shù)據(jù)恢復(fù)到某一時間點.

以上,介紹了ORACLE檢索數(shù)據(jù)一致性與事務(wù)恢復(fù)的方法。本文出自 “追求” 博客,欲與本文博主交流,請點擊這里。

【編輯推薦】

  1. 超大型Oracle數(shù)據(jù)庫應(yīng)用系統(tǒng)的設(shè)計方法
  2. 創(chuàng)建Oracle數(shù)據(jù)庫索引的三個標(biāo)準(zhǔn)
  3. Oracle性能優(yōu)化借助分區(qū)技術(shù)實現(xiàn)
  4. Oracle并發(fā)處理機(jī)制的簡單看法
  5. Oracle中被鎖定的解決辦法
責(zé)任編輯:yangsai 來源: 51CTO“追求”博客
相關(guān)推薦

2023-12-01 13:51:21

數(shù)據(jù)一致性數(shù)據(jù)庫

2017-08-25 10:16:00

2025-03-27 08:20:54

2023-09-07 08:11:24

Redis管道機(jī)制

2024-12-26 15:01:29

2023-11-20 09:28:44

2024-07-04 12:36:50

2019-11-21 10:19:45

數(shù)據(jù)應(yīng)用場景系統(tǒng)

2022-02-17 21:04:27

數(shù)據(jù)庫MysqlRedis

2021-10-13 09:55:11

流計算引擎數(shù)據(jù)

2021-10-18 10:30:59

流計算阿里云

2024-08-20 16:13:52

2023-05-26 07:34:50

RedisMySQL緩存

2021-12-05 21:06:27

軟件

2017-07-25 14:38:56

數(shù)據(jù)庫一致性非鎖定讀一致性鎖定讀

2024-12-19 21:09:38

2025-03-27 03:00:00

2023-09-24 14:35:43

Redis數(shù)據(jù)庫

2019-12-17 08:40:33

微服務(wù)架構(gòu)數(shù)據(jù)

2024-01-22 08:52:00

AQS雙異步數(shù)據(jù)一致性
點贊
收藏

51CTO技術(shù)棧公眾號