PostgreSQL Oracle 兼容性之 - rownum

標籤

PostgreSQL , rownum , Oracle 兼容性 , row_number 窗口 , limit , PPAS , EDBhtml

背景

Oracle ROWNUM是一個虛擬列,每輸出一行遞增1。ide

Oracle rownum

一般被用於LIMIT輸出記錄數。函數

SELECT ROWNUM, empno, ename, job FROM emp WHERE ROWNUM < 5 ORDER BY ename;  
   
 rownum | empno | ename |   job  
--------+-------+-------+----------  
      2 |  7499 | ALLEN | SALESMAN  
      4 |  7566 | JONES | MANAGER  
      1 |  7369 | SMITH | CLERK  
      3 |  7521 | WARD  | SALESMAN  
(4 rows)

或者用於生成序列值。post

ALTER TABLE jobhist ADD seqno NUMBER(3);  
UPDATE jobhist SET seqno = ROWNUM;
SELECT seqno, empno, TO_CHAR(startdate,'DD-MON-YY') AS start, job FROM jobhist;  
   
 seqno | empno |   start   |    job  
-------+-------+-----------+-----------  
     1 |  7369 | 17-DEC-80 | CLERK  
     2 |  7499 | 20-FEB-81 | SALESMAN  
     3 |  7521 | 22-FEB-81 | SALESMAN  
     4 |  7566 | 02-APR-81 | MANAGER  
     5 |  7654 | 28-SEP-81 | SALESMAN  
     6 |  7698 | 01-MAY-81 | MANAGER  
     7 |  7782 | 09-JUN-81 | MANAGER  
     8 |  7788 | 19-APR-87 | CLERK  
     9 |  7788 | 13-APR-88 | CLERK  
    10 |  7788 | 05-MAY-90 | ANALYST  
    11 |  7839 | 17-NOV-81 | PRESIDENT  
    12 |  7844 | 08-SEP-81 | SALESMAN  
    13 |  7876 | 23-MAY-87 | CLERK  
    14 |  7900 | 03-DEC-81 | CLERK  
    15 |  7900 | 15-JAN-83 | CLERK  
    16 |  7902 | 03-DEC-81 | ANALYST  
    17 |  7934 | 23-JAN-82 | CLERK  
(17 rows)

PostgreSQL rownum

PostgreSQL 目前沒有rownum虛擬列,可是實現一樣的功能確很容易:ui

一、輸出行號,使用臨時序列spa

postgres=# create temp sequence if not exists tmp_seq;  
  
postgres=# alter sequence tmp_seq restart with 1;  
  
postgres=# select nextval('tmp_seq') as rownum, * from test limit 10;  
 rownum | id | info |          crt_time            
--------+----+------+----------------------------  
      1 |  1 | test | 2018-01-24 11:06:24.882708  
      2 |  2 | test | 2018-01-24 11:06:24.882708  
      3 |  3 | test | 2018-01-24 11:06:24.882708  
      4 |  4 | test | 2018-01-24 11:06:24.882708  
      5 |  5 | test | 2018-01-24 11:06:24.882708  
      6 |  6 | test | 2018-01-24 11:06:24.882708  
      7 |  7 | test | 2018-01-24 11:06:24.882708  
      8 |  8 | test | 2018-01-24 11:06:24.882708  
      9 |  9 | test | 2018-01-24 11:06:24.882708  
     10 | 10 | test | 2018-01-24 11:06:24.882708  
(10 rows)

二、輸出行號,使用窗口函數rest

postgres=# select row_number() over () as rownum, * from test limit 10;  
 rownum | id | info |          crt_time            
--------+----+------+----------------------------  
      1 |  1 | test | 2018-01-24 11:06:24.882708  
      2 |  2 | test | 2018-01-24 11:06:24.882708  
      3 |  3 | test | 2018-01-24 11:06:24.882708  
      4 |  4 | test | 2018-01-24 11:06:24.882708  
      5 |  5 | test | 2018-01-24 11:06:24.882708  
      6 |  6 | test | 2018-01-24 11:06:24.882708  
      7 |  7 | test | 2018-01-24 11:06:24.882708  
      8 |  8 | test | 2018-01-24 11:06:24.882708  
      9 |  9 | test | 2018-01-24 11:06:24.882708  
     10 | 10 | test | 2018-01-24 11:06:24.882708  
(10 rows)

三、LIMIT,直接語法支持code

postgres=# select * from test limit 10;  
 id | info |          crt_time            
----+------+----------------------------  
  1 | test | 2018-01-24 11:06:24.882708  
  2 | test | 2018-01-24 11:06:24.882708  
  3 | test | 2018-01-24 11:06:24.882708  
  4 | test | 2018-01-24 11:06:24.882708  
  5 | test | 2018-01-24 11:06:24.882708  
  6 | test | 2018-01-24 11:06:24.882708  
  7 | test | 2018-01-24 11:06:24.882708  
  8 | test | 2018-01-24 11:06:24.882708  
  9 | test | 2018-01-24 11:06:24.882708  
 10 | test | 2018-01-24 11:06:24.882708  
(10 rows)

四、爲某個字段生成序列值。htm

postgres=# create temp sequence if not exists tmp_seq;  
  
postgres=# alter sequence tmp_seq restart with 1;  
  
postgres=# alter table test add column col1 int;  
ALTER TABLE  
  
postgres=# update test set col1=nextval('tmp_seq');  
UPDATE 10000000  
  
postgres=# select * from test limit 10;  
 id | info |          crt_time          | col1   
----+------+----------------------------+------  
  1 | test | 2018-01-24 11:06:24.882708 |    1  
  2 | test | 2018-01-24 11:06:24.882708 |    2  
  3 | test | 2018-01-24 11:06:24.882708 |    3  
  4 | test | 2018-01-24 11:06:24.882708 |    4  
  5 | test | 2018-01-24 11:06:24.882708 |    5  
  6 | test | 2018-01-24 11:06:24.882708 |    6  
  7 | test | 2018-01-24 11:06:24.882708 |    7  
  8 | test | 2018-01-24 11:06:24.882708 |    8  
  9 | test | 2018-01-24 11:06:24.882708 |    9  
 10 | test | 2018-01-24 11:06:24.882708 |   10  
(10 rows)

PPAS rownum

https://www.enterprisedb.com/docs/en/10.0/Ora_Compat_Dev_Guide/Database_Compatibility_for_Oracle_Developers_Guide.1.021.html#pID0E0AEK0HAget

PG的商用發行版本PPAS徹底實現了與Oracle同樣的rownum功能。

小結

主要看業務到底用Oracle的rownum幹什麼,才能因地制宜,使用對應的PG語法來支持對應的場景。

固然,目前PG的商用發行版本PPAS(EnterpriseDB)的rownum功能已經徹底和Oracle同樣了。若是業務上要節約改造時間,使用PPAS是最好的選擇。

參考

https://www.enterprisedb.com/docs/en/10.0/Ora_Compat_Dev_Guide/Database_Compatibility_for_Oracle_Developers_Guide.1.021.html#pID0E0AEK0HA

閱讀原文http://click.aliyun.com/m/41124/

相關文章
相關標籤/搜索