A pseudocolumn behaves like a table column,
but is not actually stored in the table. You can select from
pseudocolumns, but you cannot insert, update, or delete their values.
List of Pseudocolumns:
List of Pseudocolumns:
1.
Hierarchical Query Pseudocolumns
2.
Sequence Pseudocolumns
3.
Version Query Pseudocolumns
4.
COLUMN_VALUE Pseudocolumn
5.
OBJECT_ID Pseudocolumn
6.
OBJECT_VALUE Pseudocolumn
7.
ORA_ROWSCN Pseudocolumn
8.
ROWID Pseudocolumn
9.
ROWNUM Pseudocolumn
10. XMLDATA
Pseudocolumn
===========================
You can specify
CONNECT_BY_ISLEAF Example The following example shows the first three levels of the
To define a hierarchical relationship in a query, you must use the
==========================
Before you use
Within a single SQL statement containing a reference to
If any of these locations contains references to both
A sequence can be accessed by many users concurrently with no waiting or locking.
Finding the next value of a sequence: Example This example selects the next value of the employee sequence in the sample schema
===========================
Hierarchical Query Pseudocolumns
The hierarchical query pseudocolumns are valid only in hierarchical queries. The hierarchical query pseudocolumns are:-
CONNECT_BY_ISCYCLE Pseudocolumn
-
CONNECT_BY_ISLEAF Pseudocolumn
-
LEVEL Pseudocolumn
CONNECT_BY_ISCYCLE Pseudocolumn
TheCONNECT_BY_ISCYCLE pseudocolumn returns 1 if the current row has a child which is also its ancestor. Otherwise it returns 0.You can specify
CONNECT_BY_ISCYCLE only if you have specified the NOCYCLE parameter of the CONNECT BY clause. NOCYCLE enables Oracle to return the results of a query that would otherwise fail because of a CONNECT BY loop in the data.
See Also:
"Hierarchical Queries" for more information about the NOCYCLE parameter and "Hierarchical Query Examples" for an example that uses the CONNECT_BY_ISCYCLE pseudocolumnCONNECT_BY_ISLEAF Pseudocolumn
TheCONNECT_BY_ISLEAF pseudocolumn returns 1 if the current row is a leaf of the tree defined by the CONNECT BY
condition. Otherwise it returns 0. This information indicates whether a
given row can be further expanded to show more of the hierarchy.CONNECT_BY_ISLEAF Example The following example shows the first three levels of the
hr.employees table, indicating for each row whether it is a leaf row (indicated by 1 in the IsLeaf column) or whether it has child rows (indicated by 0 in the IsLeaf column):SELECT last_name "Employee", CONNECT_BY_ISLEAF "IsLeaf", LEVEL, SYS_CONNECT_BY_PATH(last_name, '/') "Path" FROM employees WHERE LEVEL <= 3 AND department_id = 80 START WITH employee_id = 100 CONNECT BY PRIOR employee_id = manager_id AND LEVEL <= 4; Employee IsLeaf LEVEL Path --------------- ---------- ---------- ----------------------------------- Russell 0 2 /King/Russell Tucker 1 3 /King/Russell/Tucker Bernstein 1 3 /King/Russell/Bernstein Hall 1 3 /King/Russell/Hall Olsen 1 3 /King/Russell/Olsen Cambrault 1 3 /King/Russell/Cambrault Tuvault 1 3 /King/Russell/Tuvault Partners 0 2 /King/Partners King 1 3 /King/Partners/King Sully 1 3 /King/Partners/Sully McEwen 1 3 /King/Partners/McEwen Smith 1 3 /King/Partners/Smith Doran 1 3 /King/Partners/Doran Sewall 1 3 /King/Partners/Sewall Errazuriz 0 2 /King/Errazuriz Vishney 1 3 /King/Errazuriz/Vishney ... 34 rows selected.
=========================
LEVEL Pseudocolumn
For each row returned by a hierarchical query, theLEVEL pseudocolumn returns 1 for a root row, 2 for a child of a root, and so on. A root row is the highest row within an inverted tree. A child row is any nonroot row. A parent row is any row that has children. A leaf row is any row without children. Figure 3-1 shows the nodes of an inverted tree with their LEVEL values.To define a hierarchical relationship in a query, you must use the
START WITH and CONNECT BY clauses.==========================
Sequence Pseudocolumns
A sequence is a schema object that can generate unique sequential values. These values are often used for primary and unique keys. You can refer to sequence values in SQL statements with these pseudocolumns:-
CURRVAL: Returns the current value of a sequence -
NEXTVAL: Increments the sequence and returns the next value
CURRVAL and NEXTVAL with the name of the sequence:sequence.CURRVAL sequence.NEXTVALTo refer to the current or next value of a sequence in the schema of another user, you must have been granted either
SELECT object privilege on the sequence or SELECT ANY SEQUENCE system privilege, and you must qualify the sequence with the schema containing it:schema.sequence.CURRVAL schema.sequence.NEXTVALTo refer to the value of a sequence on a remote database, you must qualify the sequence with a complete or partial name of a database link:
schema.sequence.CURRVAL@dblink schema.sequence.NEXTVAL@dblink
======================
Where to Use Sequence Values
You can useCURRVAL and NEXTVAL in the following locations:-
The select list of a
SELECTstatement that is not contained in a subquery, materialized view, or view
-
The select list of a subquery in an
INSERTstatement
-
The
VALUESclause of anINSERTstatement
-
The
SETclause of anUPDATEstatement
CURRVAL and NEXTVAL in the following constructs:-
A subquery in a
DELETE,SELECT, orUPDATEstatement
-
A query of a view or of a materialized view
-
A
SELECTstatement with theDISTINCToperator
-
A
SELECTstatement with aGROUPBYclause orORDERBYclause
-
A
SELECTstatement that is combined with anotherSELECTstatement with theUNION,INTERSECT, orMINUSset operator
-
The
WHEREclause of aSELECTstatement
-
The
DEFAULTvalue of a column in aCREATETABLEorALTERTABLEstatement
-
The condition of a
CHECKconstraint
CURRVAL or NEXTVAL, all referenced LONG columns, updated tables, and locked tables must be located on the same database.===========
How to Use Sequence Values
When you create a sequence, you can define its initial value and the increment between its values. The first reference toNEXTVAL returns the initial value of the sequence. Subsequent references to NEXTVAL increment the sequence value by the defined increment and return the new value. Any reference to CURRVAL always returns the current value of the sequence, which is the value returned by the last reference to NEXTVAL.Before you use
CURRVAL for a sequence in your session, you must first initialize the sequence with NEXTVAL. Please refer to CREATE SEQUENCE for information on sequences.Within a single SQL statement containing a reference to
NEXTVAL, Oracle increments the sequence once:-
For each row returned by the outer query block of a
SELECTstatement. Such a query block can appear in the following places:
-
A top-level
SELECTstatement
-
An
INSERT...SELECTstatement (either single-table or multitable). For a multitable insert, the reference toNEXTVALmust appear in theVALUESclause, and the sequence is updated once for each row returned by the subquery, even thoughNEXTVALmay be referenced in multiple branches of the multitable insert.
-
A
CREATETABLE...ASSELECTstatement
-
A
CREATEMATERIALIZEDVIEW...ASSELECTstatement
-
A top-level
-
For each row updated in an
UPDATEstatement
-
For each
INSERTstatement containing aVALUESclause
-
For each row merged by a
MERGEstatement. The reference toNEXTVALcan appear in themerge_insert_clauseor themerge_update_clauseor both. TheNEXTVALUEvalue is incremented for each row updated and for each row inserted, even if the sequence number is not actually used in the update or insert operation. IfNEXTVALis specified more than once in any of these locations, then the sequence is incremented once for each row and returns the same value for all occurrences ofNEXTVALfor that row.
NEXTVAL, then Oracle increments the sequence once and returns the same value for all occurrences of NEXTVAL.If any of these locations contains references to both
CURRVAL and NEXTVAL, then Oracle increments the sequence and returns the same value for both CURRVAL and NEXTVAL.A sequence can be accessed by many users concurrently with no waiting or locking.
Finding the next value of a sequence: Example This example selects the next value of the employee sequence in the sample schema
hr:SELECT employees_seq.nextval
FROM DUAL;
Inserting sequence values into a table: Example This example increments the employee sequence and uses its value for a new employee inserted into the sample table hr.employees:INSERT INTO employees VALUES (employees_seq.nextval, 'John', 'Doe', 'jdoe', '555-1212', TO_DATE(SYSDATE), 'PU_CLERK', 2500, null, null, 30);Reusing the current value of a sequence: Example This example adds a new order with the next order number to the master order table. It then adds suborders with this number to the detail order table:
INSERT INTO orders (order_id, order_date, customer_id) VALUES (orders_seq.nextval, TO_DATE(SYSDATE), 106); INSERT INTO order_items (order_id, line_item_id, product_id) VALUES (orders_seq.currval, 1, 2359); INSERT INTO order_items (order_id, line_item_id, product_id) VALUES (orders_seq.currval, 2, 3290); INSERT INTO order_items (order_id, line_item_id, product_id) VALUES (orders_seq.currval, 3, 2381);
===========================
Version Query Pseudocolumns
The version query pseudocolumns are valid only in Oracle Flashback Version Query, which is a form of Oracle Flashback Query. The version query pseudocolumns are:-
VERSIONS_STARTTIME: Returns the timestamp of the first version of the rows returned by the query.
-
VERSIONS_STARTSCN: Returns the SCN of the first version of the rows returned by the query.
-
VERSIONS_ENDTIME: Returns the timestamp of the last version of the rows returned by the query.
-
VERSIONS_ENDSCN: Returns the SCN of the last version of the rows returned by the query.
-
VERSIONS_XID: For each version of each row, returns the transaction ID (aRAWnumber) of the transaction that created that row version.
-
VERSIONS_OPERATION: For each version of each row, returns a single character representing the operation that caused that row version. The values returned are I (for an insert operation), U (for an update operation) or D (for a delete operation).
COLUMN_VALUE Pseudocolumn
When you refer to anXMLTableconstruct without theCOLUMNSclause, or when you use theTABLEfunction to refer to a scalar nested table type, the database returns a virtual table with a single column. This name of this pseudocolumn isCOLUMN_VALUE.
In the context ofXMLTable, the value returned is of datatypeXMLType. For example, the following two statements are equivalent, and the output for both showsCOLUMN_VALUEas the name of the column being returned:
SELECT * FROM XMLTABLE('<a>123</a>'); COLUMN_VALUE --------------------------------------- <a>123</a> SELECT COLUMN_VALUE FROM (XMLTable('<a>123</a>')); COLUMN_VALUE ---------------------------------------- <a>123</a>In the context of aTABLEfunction, the value returned is the datatype of the collection element. The following statements create the two levels of nested tables illustrated in "Multi-level Collection Example" to show the uses ofCOLUMN_VALUEin this context:
CREATE TYPE phone AS TABLE OF NUMBER; / CREATE TYPE phone_list AS TABLE OF phone; /
The next statement usesCOLUMN_VALUEto select from thephonetype:
SELECT t.COLUMN_VALUE from table(phone(1,2,3)) t; COLUMN_VALUE ------------ 1 2 3In a nested type, you can use theCOLUMN_VALUEpseudocolumn in both the select list and theTABLEfunction:
SELECT t.COLUMN_VALUE FROM TABLE(phone_list(phone(1,2,3))) p, TABLE(p.COLUMN_VALUE) t; COLUMN_VALUE ------------ 1 2 3The keywordCOLUMN_VALUEis also the name that Oracle Database generates for the scalar value of an inner nested table without a column or attribute name, as shown in the example that follows. In this context,COLUMN_VALUEis not a pseudocolumn, but an actual column name.
CREATE TABLE my_customers ( cust_id NUMBER, name VARCHAR2(25), phone_numbers phone_list, credit_limit NUMBER) NESTED TABLE phone_numbers STORE AS outer_ntab (NESTED TABLE COLUMN_VALUE STORE AS inner_ntab);OBJECT_ID Pseudocolumn
TheOBJECT_IDpseudocolumn returns the object identifier of a column of an object table or view. Oracle uses this pseudocolumn as the primary key of an object table.OBJECT_IDis useful inINSTEADOFtriggers on views and for identifying the ID of a substitutable row in an object table.
Note:In earlier releases, this pseudocolumn was calledSYS_NC_OID$. That name is still supported for backward compatibility. However, Oracle recommends that you use the more intuitive nameOBJECT_ID.OBJECT_VALUE Pseudocolumn
TheOBJECT_VALUEpseudocolumn returns system-generated names for the columns of an object table,XMLTypetable, object view, orXMLTypeview. This pseudocolumn is useful for identifying the value of a substitutable row in an object table and for creating object views with theWITHOBJECTIDENTIFIERclause.
ORA_ROWSCN Pseudocolumn
For each row,ORA_ROWSCNreturns the conservative upper bound system change number (SCN) of the most recent change to the row. This pseudocolumn is useful for determining approximately when a row was last updated. It is not absolutely precise, because Oracle tracks SCNs by transaction committed for the block in which the row resides. You can obtain a more fine-grained approximation of the SCN by creating your tables with row-level dependency tracking. Please refer toCREATETABLE... NOROWDEPENDENCIES | ROWDEPENDENCIES for more information on row-level dependency tracking.
You cannot use this pseudocolumn in a query to a view. However, you can use it to refer to the underlying table when creating a view. You can also use this pseudocolumn in theWHEREclause of anUPDATEorDELETEstatement.
ORA_ROWSCNis not supported for Flashback Query. Instead, use the version query pseudocolumns, which are provided explicitly for Flashback Query. Please refer to theSELECT... flashback_query_clause for information on Flashback Query and "Version Query Pseudocolumns" for additional information on those pseudocolumns.
Restriction: This pseudocolumn is not supported for external tables.
Example The first statement below uses theORA_ROWSCNpseudocolumn to get the system change number of the last operation on theemployeestable. The second statement uses the pseudocolumn with theSCN_TO_TIMESTAMPfunction to determine the timestamp of the operation:
SELECT ORA_ROWSCN, last_name FROM employees WHERE employee_id = 188; SELECT SCN_TO_TIMESTAMP(ORA_ROWSCN), last_name FROM employees WHERE employee_id = 188;
ROWID Pseudocolumn
For each row in the database, theROWIDpseudocolumn returns the address of the row. Oracle Database rowid values contain information necessary to locate a row:
-
The data object number of the object
-
The data block in the datafile in which the row resides
-
The position of the row in the data block (first row is 0)
-
The datafile in which the row resides (first file is 1). The file number is relative to the tablespace.
Values of theROWIDpseudocolumn have the datatypeROWIDorUROWID. Please refer to "ROWID Datatype" and "UROWID Datatype" for more information.
Rowid values have several important uses:
-
They are the fastest way to access a single row.
-
They can show you how the rows in a table are stored.
-
They are unique identifiers for rows in a table.
ROWIDas the primary key of a table. If you delete and reinsert a row with the Import and Export utilities, for example, then its rowid may change. If you delete a row, then Oracle may reassign its rowid to a new row inserted later.
Although you can use theROWIDpseudocolumn in theSELECTandWHEREclause of a query, these pseudocolumn values are not actually stored in the database. You cannot insert, update, or delete a value of theROWIDpseudocolumn.
Example This statement selects the address of all rows that contain data for employees in department 20:
SELECT ROWID, last_name FROM employees WHERE department_id = 20;
ROWNUM Pseudocolumn
For each row returned by a query, theROWNUMpseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has aROWNUMof 1, the second has 2, and so on.
You can useROWNUMto limit the number of rows returned by a query, as in this example:
SELECT * FROM employees WHERE ROWNUM < 10; If anORDERBYclause followsROWNUMin the same query, then the rows will be reordered by theORDERBYclause. The results can vary depending on the way the rows are accessed. For example, if theORDERBYclause causes Oracle to use an index to access the data, then Oracle may retrieve the rows in a different order than without the index. Therefore, the following statement will not have the same effect as the preceding example:
SELECT * FROM employees WHERE ROWNUM < 11 ORDER BY last_name; If you embed theORDERBYclause in a subquery and place theROWNUMcondition in the top-level query, then you can force theROWNUMcondition to be applied after the ordering of the rows. For example, the following query returns the employees with the 10 smallest employee numbers. This is sometimes referred to as top-N reporting:
SELECT * FROM (SELECT * FROM employees ORDER BY employee_id) WHERE ROWNUM < 11; In the preceding example, theROWNUMvalues are those of the top-levelSELECTstatement, so they are generated after the rows have already been ordered byemployee_idin the subquery.
Conditions testing forROWNUMvalues greater than a positive integer are always false. For example, this query returns no rows:
SELECT * FROM employees WHERE ROWNUM > 1; The first row fetched is assigned aROWNUMof 1 and makes the condition false. The second row to be fetched is now the first row and is also assigned aROWNUMof 1 and makes the condition false. All rows subsequently fail to satisfy the condition, so no rows are returned.
You can also useROWNUMto assign unique values to each row of a table, as in this example:
UPDATE my_table SET column1 = ROWNUM; -
The data object number of the object
XMLDATA Pseudocolumn
Oracle storesXMLTypedata either in LOB or object-relational columns, based on XMLSchema information and how you specify the storage clause. TheXMLDATApseudocolumn lets you access the underlying LOB or object relational column to specify additional storage clause parameters, constraints, indexes, and so forth.Example The following statements illustrate the use of this pseudocolumn. Suppose you create a simple table ofXMLType:CREATE TABLE xml_lob_tab of XMLTYPE;
The default storage is in aCLOBcolumn. To change the storage characteristics of the underlying LOB column, you can use the following statement:ALTER TABLE xml_lob_tab MODIFY LOB (XMLDATA) (STORAGE (BUFFER_POOL DEFAULT) CACHE);
Now suppose you have created an XMLSchema-based table like thexwarehousestable created in "Using XML in SQL Statements". You could then use theXMLDATAcolumn to set the properties of the underlying columns, as shown in the following statement:ALTER TABLE xwarehouses ADD (UNIQUE(XMLDATA."WarehouseId"));