oracle SQL简介(四)

来源:互联网 发布:ubuntu如何读取u盘 编辑:程序博客网 时间:2024/05/16 01:53
函数,树结构查询,连接查询,子查询, 优化

6.   top n查询

select rownum rn ,a.* from (select * from emp order by sal desc ) a where rownum<=3;

 

select b.* from (select rownum rn ,a.* from (select * from emp order by sal desc ) a where rownum<=10 ) b where rn>=3;

 

select * from (select rownum rn,b.* from (select a.* from emp a order by sal desc) b )

where rn between 3 and 10;

 

分析函数的top-n查询:

select b.* from (select row_number() over ( order by sal desc) rn,a.* from emp a) b

where rn between 3 and 10;

 

7.   序列

create sequence s_emp

increment by 1

start with 1

 maxvalue 99999999

 nominvalue

nocycle

 nocache

noorder;

 

解释:

create sequence sequence_name

increment by 1 --表示从1开始计值

start with 1  --每次增长1

nomaxvalue / maxvalue 999999 --有两个可选值,要么无最大值,要么指定最大值

minvalue 1 / nominvalue  --maxvalue

cycle --表示达到最大值后从头开始,也可以为nocycle

cache 10 --指定cache的值。如果指定CACHE值,oracle就可以预先在内

存里面放置一些sequence,这样存取的快些。cache里面的取完后,oracle

自动再取一组到cache。使用cache或许会跳号, 比如数据库突然不正常down

掉(shutdown abort),cache中的

sequence就会丢失. 所以可以在create sequence的时候用nocache防止这种情况。

order;--指定排序

序列提供两个方法:

nextvalcurrvalnextval是取序列的下一个值,一次nextval会增加一次sequence的值;currval是取序列的当前值。

第一次nextval返回的是初始值;随后的nextval会自动增加你定义的INCREMENT BY值,然后返回增加后的值。currval总是返回当前sequence的值,但是在第一次nextval初始化之后才可以使用currval,否则会出现出错。

 

select s_emp.nextval from dual;

select s_emp.currval from dual;

原创粉丝点击