zoukankan      html  css  js  c++  java
  • 伪列SQL Pseudocolumns

    ORACLE中常见的伪列的英文解释:

    官方网站:http://docs.oracle.com/cd/B10501_01/appdev.920/a96624/06_ora.htm#1990

    SQL Pseudocolumns

    PL/SQL recognizes the following SQL pseudocolumns, which return specific data items: CURRVAL, LEVEL, NEXTVAL, ROWID, and ROWNUM. Pseudocolumns are not actual columns in a table but they behave like columns. For example, you can select values from a pseudocolumn. However, you cannot insert into, update, or delete from a pseudocolumn. Also, pseudocolumns are allowed in SQL statements, but not in procedural statements.

    CURRVAL and NEXTVAL

    A sequence is a schema object that generates sequential numbers. When you create a sequence, you can specify its initial value and an increment. CURRVAL returns the current value in a specified sequence.

    Before you can reference CURRVAL in a session, you must use NEXTVAL to generate a number. A reference to NEXTVAL stores the current sequence number in CURRVAL. NEXTVAL increments the sequence and returns the next value. To obtain the current or next value in a sequence, you must use dot notation, as follows:

    sequence_name.CURRVAL
    sequence_name.NEXTVAL

    After creating a sequence, you can use it to generate unique sequence numbers for transaction processing. However, you can use CURRVAL and NEXTVAL only in a SELECT list, the VALUES clause, and the SET clause. In the following example, you use a sequence to insert the same employee number into two tables:

    INSERT INTO emp VALUES (empno_seq.NEXTVAL, my_ename, ...);
    INSERT INTO sals VALUES (empno_seq.CURRVAL, my_sal, ...);

    If a transaction generates a sequence number, the sequence is incremented immediately whether you commit or roll back the transaction.

    LEVEL

    You use LEVEL with the SELECT CONNECT BY statement to organize rows from a database table into a tree structure. LEVEL returns the level number of a node in a tree structure. The root is level 1, children of the root are level 2, grandchildren are level 3, and so on.

    In the START WITH clause, you specify a condition that identifies the root of the tree. You specify the direction in which the query walks the tree (down from the root or up from the branches) with the PRIOR operator.

    ROWID

    ROWID returns the rowid (binary address) of a row in a database table. You can use variables of type UROWID to store rowids in a readable format. In the following example, you declare a variable named row_id for that purpose:

    DECLARE
    row_id UROWID;

    When you select or fetch a physical rowid into a UROWID variable, you can use the function ROWIDTOCHAR, which converts the binary value to an 18-byte character string. Then, you can compare the UROWID variable to the ROWID pseudocolumn in the WHERE clause of an UPDATE or DELETE statement to identify the latest row fetched from a cursor. For an example, see "Fetching Across Commits".

    ROWNUM

    ROWNUM returns a number indicating the order in which a row was selected from a table. The first row selected has a ROWNUM of 1, the second row has a ROWNUM of 2, and so on. If a SELECT statement includes an ORDER BY clause, ROWNUMs are assigned to the retrieved rows before the sort is done.

    You can use ROWNUM in an UPDATE statement to assign unique values to each row in a table. Also, you can use ROWNUM in the WHERE clause of a SELECT statement to limit the number of rows retrieved, as follows:

    DECLARE
    CURSOR c1 IS SELECT empno, sal FROM emp
    WHERE sal > 2000 AND ROWNUM < 10; -- returns 10 rows

    The value of ROWNUM increases only when a row is retrieved, so the only meaningful uses of ROWNUM in a WHERE clause are

    ... WHERE ROWNUM < constant;
    ... WHERE ROWNUM <= constant;

    I believe that we are who we choose to be. Nobody‘s going to come and save you, you‘ve got to save yourself. 我相信我们成为怎样的人是我们自己的选择。没有人会来拯救你,你必须要自己拯救自己。
  • 相关阅读:
    基于Ubuntu + nextCloud 搭建自己的私人网盘
    基于Ubuntu部署 memcached 服务
    基于Ubuntu搭建Seafile专属网盘
    基于ubuntu搭建 Discuz 论坛
    基于ubuntu搭建 WordPress 个人博客
    基于CentOS 搭建 FTP 文件服务
    搭建 WordPress 个人博客
    Scale-out NAS 和scale-up NAS 系统的优缺点
    鱼缸的启示:Scale-out和Scale-up架构
    整死你个妖精,CDN西游捉妖记!
  • 原文地址:https://www.cnblogs.com/caroline/p/2307954.html
Copyright © 2011-2022 走看看