ORACLE rownum用法(ZT
如何正确利用Rownum来限制查询所返回的行数?
含义解释:
1、rownum是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,
依此类推,这个伪字段可以用于限制查询返回的总行数。
2、rownum不能以任何基表的名称作为前缀。
使用方法:
现有一个商品销售表sale,表结构为:
month char(6) --月份
sell number(10,2) --月销售金额
create table sale (month char(6),sell number);
insert into sale values('200001',1000);
insert into sale values('200002',1100);
insert into sale values('200003',1200);
insert into sale values('200004',1300);
insert into sale values('200005',1400);
insert into sale values('200006',1500);
insert into sale values('200007',1600);
insert into sale values('200101',1100);
insert into sale values('200202',1200);
insert into sale values('200301',1300);
insert into sale values('200008',1000);
commit;
SQL> select rownum,month,sell from sale where rownum=1;(可以用在限制返回记录条数的地方,保证不出错,如:隐式游标)
ROWNUM MONTH SELL
--------- ------ ---------
1 200001 1000
SQL> select rownum,month,sell from sale where rownum=2;(1以上都查不到记录)
没有查到记录
SQL> select rownum,month,sell from sale where rownum>5;
(由于rownum是一个总是从1开始的伪列,Oracle 认为这种条件不成立,查不到记录)
没有查到记录
只返回前3条纪录
SQL> select rownum,month,sell from sale where rownum<4;
ROWNUM MONTH SELL
--------- ------ ---------
1 200001 1000
2 200002 1100
3 200003 1200
如何用rownum实现大于、小于逻辑?(返回rownum在4—10之间的数据)(minus操作,速度会受影响)
SQL> select rownum,month,sell from sale where rownum<10
2 minus
3 select rownum,month,sell from sale where rownum<5;
ROWNUM MONTH SELL
--------- ------ ---------
5 200005 1400
6 200006 1500
7 200007 1600
8 200101 1
相关文档:
TO_DATE格式(以时间:2007-11-02 13:45:25为例)
Year:
yy two digits 两位年 ......
CSDN里的一个朋友问到了这个索引覆盖的概念。 这个概念很小的知识点,在我的论坛里有解释“”,不过作为Oracle版主,不能在回帖里加上网外的地址链接,所以这里在CSDN里帖上一份
比如有复合索引为3个字段:f1 + f2 + f3,请问:
1: select f1, f2, f3, f4 from table where f1 = 'XX' and f2 = 'XX'. ......
大家在应用ORACLE的时候可能会遇到很多看起来不难的问题, 特别对新手来说, 今天我简单把它总结一下, 发布给大家, 希望对大家有帮助! 和大家一起探讨, 共同进步!
对ORACLE高手来说是不用看的.
1. Oracle安装完成后的初始口令?
internal/oracle
sys/change_on_install
system/manager
......
1、默认事例:
用户名:scott密码:tiger 主机字符串:本机可以为空
2、启动方法:
运行:sqlplus scott/tiger@lhd
3、SQLPLUS基本命令:
Desc:显示表、视图结构 desc 表名,视图
List:列出SQL缓冲区区中的一行或多行命令语句
Exit:退出
4、常用的数据字典(三种前缀:USER,ALL,DBA)
USER_TABLE ......
1、创建表t1 :create table t1 (id number,name nvarchar(8));
2、创建序列 :CREATE SEQUENCE t1_id INCREMENT BY 1 START WITH 1 MAXVALUE
1.0E28 MINVALUE 1 NOCYCLE CACHE 20 NOORDER
3. 创建触发器 :
CREATE TRIGGER tig_insert_t1
BEFORE INSERT ON "YINZQ"."T1"
begin
if (:new.id is null) then
......