In an Oracle database, what is the difference betw

2019-02-01 21:17发布

What is the difference between ROWNUM and ROW_NUMBER ?

5条回答
Explosion°爆炸
2楼-- · 2019-02-01 21:44

From a little reading, ROWNUM is a value automatically assigned by Oracle to a rowset (prior to ORDER BY being evaluated, so don't ever ORDER BY ROWNUM or use a WHERE ROWNUM < 10 with an ORDER BY).

ROW_NUMBER() appears to be a function for assigning row numbers to a result set returned by a subquery or partition.

查看更多
Viruses.
3楼-- · 2019-02-01 21:45

Apart from the other differences mentioned in answers, you should also consider performance. There is a non-authoritative but very interesting report here, comparing various means of pagination, among which the use of ROWNUM compared to ROW_NUMBER() OVER():

http://www.inf.unideb.hu/~gabora/pagination/results.html

查看更多
我想做一个坏孩纸
4楼-- · 2019-02-01 21:52

Rownum starts with 1 ..increases after condition evaluated results to true . Hence rownum >=1 returns all rows in table

查看更多
手持菜刀,她持情操
5楼-- · 2019-02-01 21:53

rownum is a pseudocolumn which can be added to any select query, to number the rows returned (starting with 1). They are ordered according to when they were identified as being part of the final result set. (#ref)

row_number is an analytic's function, which can be used to number the rows returned by the query in an order mandated by the row_number() function.

查看更多
混吃等死
6楼-- · 2019-02-01 21:58

ROWNUM is a "pseudocolumn" that assigns a number to each row returned by a query:

SQL> select rownum, ename, deptno
  2  from emp;

    ROWNUM ENAME          DEPTNO
---------- ---------- ----------
         1 SMITH              99
         2 ALLEN              30
         3 WARD               30
         4 JONES              20
         5 MARTIN             30
         6 BLAKE              30
         7 CLARK              10
         8 SCOTT              20
         9 KING               10
        10 TURNER             30
        11 FORD               20
        12 MILLER             10

ROW_NUMBER is an analytic function that assigns a number to each row according to its ordering within a group of rows:

SQL> select ename, deptno, row_number() over (partition by deptno order by ename) rn
  2  from emp;

ENAME          DEPTNO         RN
---------- ---------- ----------
CLARK              10          1
KING               10          2
MILLER             10          3
FORD               20          1
JONES              20          2
SCOTT              20          3
ALLEN              30          1
BLAKE              30          2
MARTIN             30          3
TURNER             30          4
WARD               30          5
SMITH              99          1
查看更多
登录 后发表回答