6 Nov 2011

Pseudo columns vs Virtual columns in Oracle

Pseudo columns vs Virtual columns in Oracle


What is Pseudo column

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
 The pseudocolumns CURRVAL, NEXTVAL, LEVEL, PRIOR, and ROWNUM, or date constants that are not fully specified.

Ex:

ROWID Pseudo column
 For each row in the database, the ROWID pseudo column 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.
 Usually, a rowid value uniquely identifies a row in the database. However, rows in different tables that are stored together in the same cluster can have the same rowid.
 Values of the ROWID pseudocolumn have the datatype ROWID or UROWID. 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.
 You should not use ROWID as 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 the ROWID pseudocolumn in the SELECT and WHERE clause of a query, these pseudocolumn values are not actually stored in the database. You cannot insert, update, or delete a value of the ROWID pseudocolumn.
 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;


What is Virtual Column?

Virtual columns appear to be normal table columns, but their values are derived rather than being stored on disc. The syntax for creating the same has shown below.

 CREATE TABLE test_employees (
   id          NUMBER,
   first_name  VARCHAR2(10),
   last_name   VARCHAR2(10),
   user_name GENERATED ALWAYS AS (first_name||'_'||last_name)
 );



If the data type is omitted, it is determined based on the result of the expression. The GENERATED