当前所在位置:珠峰网资料 >> 计算机 >> Oracle认证 >> 正文
2015年Oracle认证考试辅导:Oracle检索数据一致性与事务恢复
发布时间:2010/3/13 11:33:55 来源:城市学习网 编辑:MOON
  Oracle为了保证用户检索数据的一致性, 通过UNDO记录,当用户检索数据库数据时,Oracle总是使用户只能看到被提交过的数据或特定时间点的数据(select语句时间点),UNDO记录会被存放到回滚段中,假如该数据未提交,用户检索数据时,都是从UNDO记录中取得的。(如下)
  先打开一个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
  -
  110
  3 4
  结论:如果用户修改数据但没有提交,其它用户检索的都是UNDO段的数据,这样就保证了数据的一致性
  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
  -
  110
  3 4
  可见当用户用命今rollback还能回滚到初始状态。
  2.当用户updata数据且已提交
  当用户updata数据且已提交后,可以根据SCN记录把数据还源。
  先查看原始数据
  SQL select * from c;
  A B
  110
  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
  110
  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
  110
  3 4
  可见可以根据SCN恢复到某一检查点的数据,如果把SCN转换成时间,,就可以把数据恢复到某一时间点。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved