Oracle 11g introduced the concept of ‘Virtual Column’ within a table.
Virtual Columns are similar to normal table’s columns but with the following differences:
They are defined by an expression. The result of evaluation of this expression becomes the value of the column.
The values of the virtual column are not stored in the database. Rather, it’s computed at run-time
when you query the data.
You can’t update (in SET clause of update statement) the values of virtual column. These are read
only values, that are computed dynamically and any attempt to modify them will result into oracle error.
The syntax for defining a virtual column is:
column_name [datatype] [GENERATED ALWAYS] AS [expression] [VIRTUAL]
Points :-
Virtual columns can be used in the WHERE clause of UPDATE and DELETE statement but they can’t be modified by DML.
Statistics can be collected on them.
They can be used as a partition key in virtual column based partitioning.
Indexes can be created on them. As you might have guessed, oracle would create function based
indexes as we create on normal tables.
Constraints can be created on them.
CREATE TABLE EMPLOYEE
(
empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) GENERATED ALWAYS AS (monthly_sal*12 + bonus)
);
Here we have defined a virtual column “total_sal” whose value would be dynamically calculated;
SELECT column_name, data_type, data_length, data_default, virtual_column
FROM user_tab_cols
WHERE table_name = 'EMPLOYEE';
COLUMN_NAME | DATA_TYPE | DATA_LENGTH | DATA_DEFAULT | VIRTUAL_COLUMN
EMPL_ID | NUMBER | 22 | null | NO
EMPL_NM | VARCHAR2 | 50 | null | NO
MONTHLY_SAL | NUMBER | 22 | null | NO
BONUS | NUMBER | 22 | null | NO
TOTAL_SAL | NUMBER | 22 | "MONTHLY_SAL"*12+"BONUS" | YES
LIMITATIONS ON VIRTUAL COLUMNS:-
A virtual column can only be of scalar datatype or XMLDATATYE. It can’t be a user defined type, LOB or RAW.
All columns mentioned as part of the virtual column expression should belong to the same table.
No DMLs are allowed on the virtual columns.
The virtual column expression can’t reference any other virtual column.
Virtual columns can only be created on ordinary tables. They can’t be created on index-organized,
external, object, cluster or temporary tables.
If a deterministic function is used as virtual column expression, that virtual column can’t be used as
a partitioning key for virtual column-based partitioning.
Virtual Columns are similar to normal table’s columns but with the following differences:
They are defined by an expression. The result of evaluation of this expression becomes the value of the column.
The values of the virtual column are not stored in the database. Rather, it’s computed at run-time
when you query the data.
You can’t update (in SET clause of update statement) the values of virtual column. These are read
only values, that are computed dynamically and any attempt to modify them will result into oracle error.
The syntax for defining a virtual column is:
column_name [datatype] [GENERATED ALWAYS] AS [expression] [VIRTUAL]
Points :-
Virtual columns can be used in the WHERE clause of UPDATE and DELETE statement but they can’t be modified by DML.
Statistics can be collected on them.
They can be used as a partition key in virtual column based partitioning.
Indexes can be created on them. As you might have guessed, oracle would create function based
indexes as we create on normal tables.
Constraints can be created on them.
CREATE TABLE EMPLOYEE
(
empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) GENERATED ALWAYS AS (monthly_sal*12 + bonus)
);
Here we have defined a virtual column “total_sal” whose value would be dynamically calculated;
SELECT column_name, data_type, data_length, data_default, virtual_column
FROM user_tab_cols
WHERE table_name = 'EMPLOYEE';
COLUMN_NAME | DATA_TYPE | DATA_LENGTH | DATA_DEFAULT | VIRTUAL_COLUMN
EMPL_ID | NUMBER | 22 | null | NO
EMPL_NM | VARCHAR2 | 50 | null | NO
MONTHLY_SAL | NUMBER | 22 | null | NO
BONUS | NUMBER | 22 | null | NO
TOTAL_SAL | NUMBER | 22 | "MONTHLY_SAL"*12+"BONUS" | YES
LIMITATIONS ON VIRTUAL COLUMNS:-
A virtual column can only be of scalar datatype or XMLDATATYE. It can’t be a user defined type, LOB or RAW.
All columns mentioned as part of the virtual column expression should belong to the same table.
No DMLs are allowed on the virtual columns.
The virtual column expression can’t reference any other virtual column.
Virtual columns can only be created on ordinary tables. They can’t be created on index-organized,
external, object, cluster or temporary tables.
If a deterministic function is used as virtual column expression, that virtual column can’t be used as
a partitioning key for virtual column-based partitioning.
No comments:
Post a Comment