当前位置:首页 > oracle中常用的一些语句--增删改查(精)
如何查找、删除表中重复的记录 方法原理:
1、Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的, rowid确定了每条记录是在ORACLE中的哪一个数据文件、块、行上。 2、在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中
那些具有最大rowid的就可以了,其余全部删除。 实现方法:
SQL》 create table a ( 2 bm char(4), --编码 3 mc varchar2(20) --名称 4 ) 5 / 表已建立。
SQL》 insert into a values(‘1111’,‘1111’); SQL》 insert into a values(‘1112’,‘1111’); SQL》 insert into a values(‘1113’,‘1111’); SQL》 insert into a values(‘1114’,‘1111’); SQL》 insert into a select * from a; 插入4个记录。 SQL》 commit; 完全提交。
SQL》 select rowid,bm,mc from a; ROWID BM MC
------------------ ---- -------
000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111 000000D5.0003.0002 1114 1111 000000D5.0004.0002 1111 1111 000000D5.0005.0002 1112 1111 000000D5.0006.0002 1113 1111 000000D5.0007.0002 1114 1111 查询到8记录。 查出重复记录
SQL》 select rowid,bm,mc from a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);
ROWID BM MC
------------------ ---- -------------------- 000000D5.0000.0002 1111 1111 000000D5.0001.0002 1112 1111 000000D5.0002.0002 1113 1111 000000D5.0003.0002 1114 1111 删除重复记录
SQL》 delete from a a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);
删除4个记录。
SQL》 select rowid,bm,mc from a; ROWID BM MC
------------------ ---- --------------------
000000D5.0004.0002 1111 1111 000000D5.0005.0002 1112 1111 000000D5.0006.0002 1113 1111 000000D5.0007.0002 1114 1111 desc table;检查表结构
select * from tab where tabtype=‘TABLE’;显示当前用户下的所有表。 select count(*) from table;显示此表的数据行数; spool c:\\tony.txt;日记路径
spool off;关闭记录后可以看到日记文件里的内容。 alter table stu add(classid number(2));添加字段 alter table stu modify(xm varchar2(12));修改字段的长度 alter table stu drop column sal; drop table stu; rename student to stu;
alter table student drop column sal; alter table stu add(salary number(7,2));
insert into stu values(‘A001’,‘张三’,‘男’,‘01-5月-05’,10);
insert into stu(xh,xm,sex) values (‘A003’,‘JOHN’,‘女’); insert into student(xh,xm,sex,birthday) values (‘A004’,‘MARTIN’,‘男’,null);
修改 update
update stu set sex=‘女’ where xh=‘A001’;
update student set sex=‘男’,birthday=‘1980-04-01’where xh=‘A001’;
update student set classid=20 where birthday is null;
delete from stu;drop table student;delete from stu where xh=‘A001’; truncate table stu;删除表中的所有记录,表结构还在不写日记无法找回记录 select * from stu;
select * from student where classid like ‘1%’; select * from student where xh like ‘%A%’; select * from student where xh like ‘A%’; select * from student where xh like ‘%A’; select * from student where xh = ‘A%’; select * from student order by birthday;
select * from student order by birthday desc,xh asc; --按birthday 降序 按xh升序(asc/默认)
select * from student where sex=‘女’ or birthday=‘1999-02-01’; select * from student where sex=‘女’ and birthday=‘1999-02-01’; select * from student where salary 》 20 and xh 《》 ‘B002’; (!=) oracle 函数的学习
单行函数 返回值只有一个 分组函数 返回值是多条记录 group by sum avg
select sysdate from dual;dual哑元素 没有表需要查询的时候 select xm||‘-----’||classid from stu; select 1+1 from dual;
共分享92篇相关文档