Oracle检索数据一致性与事务恢复

来源:互联网 发布:李治廷 范冰冰 知乎 编辑:程序博客网 时间:2024/05/03 19:43

    Oracle为了保证用户检索数据的一致性, 通过UNDO记录,当用户检索数据库数据时,Oracle总是使用户只能看到被提交过的数据或特定时间点的数据(select语句时间点),UNDO记录会被存放到回滚段中,假如该数据未提交,用户检索数据时,都是从UNDO记录中取得的。(如下图:)

从UNDO记录中取得

    1. ORACLE检索数据一致性

    先打开一个SecureCRT.(第一个session)

    先建一个表

SQL> create table c(a int);
Table created.
SQL> alter table c add b number;
Table altered.
SQL> desc c
 Name                                      Null?    Type
 ----------------------------------------- -------- --------------------------------------------
 A                                                  NUMBER(38)
 B                                                  NUMBER

    表中插入数据并提交

SQL> insert into c values(1,2);
1 row created.
SQL> insert into c values(3,4);
1 row created.
SQL> select * from c;
         A          B
---------- -----------------------------
         1          2
         3          4
SQL> commit;
Commit complete.

    再打开一个SecureCRT.(第二个session)

    查询

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

    第一个session更改表中的数据但不提交

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

    第二个session查询(修改但没有提交检索的是UNDO中的数据)

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

    第一个session提交

SQL> commit;
Commit complete.

    第二个会话查询(可见只有提交后才能检索到数据段的数据)

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

    结论:如果用户修改数据但没有提交,其它用户检索的都是UNDO段的数据,这样就保证了数据的一致性

 

    2.回滚数据(事务恢复)

    1.当用户updata数据但还没有提交

SQL> select * from c;
         A          B
---------- -----------------------------
         1          10
         3          4
SQL> update c set b=2 where a=1;
SQL> select * from c;
         A          B
---------- -----------------------------
         1          2
         3          4

    这时用户突然后悔了,想恢复到原来的状态

SQL> rollback;
Rollback complete.
SQL> commit;

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

    可见当用户用命今rollback还能回滚到初始状态。

    2.当用户updata数据且已提交

    当用户updata数据且已提交后,可以根据SCN记录把数据还源。

    先查看原始数据

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

    找到SCN

SQL> select current_scn from v$database;
CURRENT_SCN
-----------
     693636

    现在删除表中的数据并提交

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

    查询(现在表中已没有数据了)

SQL> select * from c;
no rows selected

    检索特定SCN的数据

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

    恢复数据

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

    现在再查询

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

    可见可以根据SCN恢复到某一检查点的数据,如果把SCN转换成时间,,就可以把数据恢复到某一时间点。

原创粉丝点击