Oracle实用技巧 中
文章作者 100test 发表时间 2007:03:14 13:29:43
来源 100Test.Com百考试题网
4.删除表中重复记录
方法原理:
1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的, rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。
2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中那些具有最大rowid的就可以了,其余全部删除。
实现方法:
SQL> create table a(bm char(4),mc varchar2(20)).
Table created
SQL> insert into a values(’1111’,’aaaa’).
SQL> insert into a values(’1112’,’aaaa’).
SQL> insert into a values(’1113’,’aaaa’).
SQL> insert into a values(’1114’,’aaaa’).
SQL> insert into a 0select * from a.
4 rows inserted
SQL> commit.
Commit complete
SQL> 0select rowid,bm,mc from a.
ROWID BM MC
------------------ ---- --------------------
AAAIRIAAQAAAAJqAAA 1111 aaaa
AAAIRIAAQAAAAJqAAB 1112 aaaa
AAAIRIAAQAAAAJqAAC 1113 aaaa
AAAIRIAAQAAAAJqAAD 1114 aaaa
AAAIRIAAQAAAAJqAAE 1111 aaaa
AAAIRIAAQAAAAJqAAF 1112 aaaa
AAAIRIAAQAAAAJqAAG 1113 aaaa
AAAIRIAAQAAAAJqAAH 1114 aaaa
8 rows 0selected
查出重复记录
SQL> 0select rowid,bm,mc from a where a.rowid!=(0select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc).
ROWID BM MC
------------------ ---- --------------------
AAAIRIAAQAAAAJqAAA 1111 aaaa
AAAIRIAAQAAAAJqAAB 1112 aaaa
AAAIRIAAQAAAAJqAAC 1113 aaaa
AAAIRIAAQAAAAJqAAD 1114 aaaa
删除重复记录
SQL> 0delete from a a where a.rowid!=(0select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc).
删除4个记录.
SQL> 0select rowid,bm,mc from a.
ROWID BM MC
------------------ ---- --------------------
AAAIRIAAQAAAAJqAAE 1111 aaaa
AAAIRIAAQAAAAJqAAF 1112 aaaa
AAAIRIAAQAAAAJqAAG 1113 aaaa
AAAIRIAAQAAAAJqAAH 1114 aaaa
5.控制文件损坏时的恢复
根据如下错误信息,我们发现数据库只能启动实例,读控制文件时发生错误。在数据库设计的过程中,从安全的角度考虑,系统使用了三个镜像的控制文件,现在三个控制文件version号不一致。
SVRMGRL>startup
oracle instance started
total system global area 222323980 bytes
fixed size 70924 bytes
variable size 78667776 bytes
database buffers 143507456 bytes
redo buffers 77824 bytes
ORA-00214: controlfile ‘d:\oracle\oradata\orcl\control01.ctl’ version 57460 inconsistent with fit