DEV Community

mrcaption49
mrcaption49

Posted on

Pseudocolumn in a database table

A pseudocolumn in Oracle is a special built-in column-like feature that behaves like a column but isnโ€™t actually stored in the table. It can be queried just like a regular column but cannot be updated or inserted into. Examples include ROWNUM, ROWID, SYSDATE, LEVEL, and NEXTVAL/CURRVAL (from sequences). For instance, ROWNUM gives the sequence of rows returned, while ROWID provides the unique address of a row in the database. Pseudocolumns are often used in pagination, hierarchical queries, and data sequencing. They're particularly useful in filtering results, identifying specific records, or auto-generating sequence numbers. Though virtual, pseudocolumns are powerful tools in SQL querying and performance tuning.


A pseudocolumn in a database table is a special, read-only column that is not stored as part of the table data but is automatically available by the database for each row in a query.


๐Ÿ” Characteristics of a Pseudocolumn:

Not actually stored in the table.

Cannot be inserted or updated.

Acts like a regular column when you query.

Provided by the database engine, not the user.


โœ… Common Examples in Oracle:

Pseudocolumn Description

ROWNUM Returns the row number in the result set. Useful for limiting rows.
ROWID Returns the unique address (physical location) of a row in the table.
LEVEL Used in hierarchical (tree-structured) queries with CONNECT BY.
SYSDATE Returns the current system date and time (technically a function, but often used like a pseudocolumn).
NEXTVAL, CURRVAL Used to fetch the next or current value of a sequence.


๐Ÿ“˜ Example Usage:

๐Ÿ”น ROWNUM:

SELECT * FROM employees WHERE ROWNUM <= 5;

โœ… Fetches the first 5 rows from the table (not necessarily ordered).


๐Ÿ”น ROWID:

SELECT ROWID, employee_id FROM employees;

โœ… Useful for identifying or deleting specific physical rows quickly.


๐Ÿ”น LEVEL with hierarchical data:

SELECT employee_id, manager_id, LEVEL
FROM employees
CONNECT BY PRIOR employee_id = manager_id
START WITH manager_id IS NULL;

โœ… Returns the hierarchy of employees (org chart).


๐Ÿ”น Sequence pseudocolumns:

SELECT my_seq.NEXTVAL FROM dual;

โœ… Fetches the next value from a sequence for primary key generation.


๐Ÿ”’ Limitations:

You cannot insert, update, or delete from pseudocolumns.

Not all databases support the same pseudocolumns (e.g., ROWNUM is Oracle-specific).


๐Ÿง  Why use pseudocolumns?

Help in pagination, filtering, hierarchical data queries, sequence generation, debugging physical rows, etc.

Improve performance in certain situations (e.g., using ROWID for fast deletes).

Top comments (0)