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. 我相信我们成为怎样的人是我们自己的选择。没有人会来拯救你,你必须要自己拯救自己。
  • 相关阅读:
    超级好用的excel导出方法,比phpexcel快n倍,并且无乱码
    PHP生成随机数;订单号唯一
    php判断检测一个数组里有没有重复的值
    修改git 提交的用户名和用户Email命令
    利用 PHP CURL zip压缩文件上传
    Linux 重启 PHP-FPM 命令
    Postgresql 时间串转换格式
    rollup node.js 打包工具
    PHP正则表达式提取html超链接中的href地址
    解决Ubuntu系统下 mysql 远程连接失败的问题 ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.xx.xx' (110)
  • 原文地址:https://www.cnblogs.com/caroline/p/2307954.html
Copyright © 2011-2022 走看看