Virtual Columns is a new feature introduced in Oracle 11g. As the name indicates, it is virtual. It do not occupy any memory space. This article features how to create a table with virtual column, how to add one to an existing table, creating Indexes on virtual column and the usage and restriction in Virtual columns.
Syntax
Syntax to create a virtual column when creating a table:
CREATE TABLE TABLE_NAME
(COLUMN_1 DATA_TYPE,
VIRTUAL_COLUMN [DATATYPE] [GENERATED ALWAYS] AS EXPRESSION [VIRTUAL])
Explanation
While defining a virtual column datatype of this column is optional, if it is not mentioned oracle derives it from the expression. GENERATED ALWAYS is added for clarity and this in optional as well as the keyword VIRTUAL is also optional.
Example
Lets create a table with virtual column:
CREATE TABLE VIRTUAL_EG
(SALARY NUMBER,
ANUUAL_SALARY NUMBER GENERATED ALWAYS AS (12*SALARY) VIRTUAL);
Lets insert rows into the table
sh@ORCL> insert into virtual_eg
select rownum * 100
from all_objects where rownum <=1000 2 3
4 /
insert into virtual_eg
*
ERROR at line 1:
ORA-00947: not enough values
Lets insert rows into the table
sh@ORCL> insert into virtual_eg
select rownum * 100
from all_objects where rownum <=1000 2 3
4 /
insert into virtual_eg
*
ERROR at line 1:
ORA-00947: not enough values
We cannot insert values directly into a virtual column. Also We need to mention the column list while inserting into a table containing Virtual column otherwise error 'not enought values' would be shown.
So let me modify my insert statement:
SH@ORCL> INSERT INTO VIRTUAL_EG (SALARY)
SELECT ROWNUM * 100 FROM ALL_OBJECTS WHERE ROWNUM <=1000;
1000 rows created.
Let me select from the table:
select * from virtual_eg where rownum <=10;
SALARY ANUUAL_SALARY
---------- -------------
100 1200
200 2400
300 3600
400 4800
500 6000
600 7200
700 8400
800 9600
900 10800
1000 12000
We can create integrity constraints and indexes on the virtual columns:
CREATE INDEX IX_ANNUAL_SALARY ON VIRTUAL_EG(ANUUAL_SALARY)
One can add a virtual column to an existing table by using an alter statement:
ALTER TABLE VIRTUAL_EG ADD
( COMMISION AS (CASE WHEN SALARY < 5000 THEN 0.1 * SALARY
WHEN SALARY < 10000 THEN 0.15* SALARY
WHEN SALARY >= 10000 THEN 0.2 * SALARY
END ))
Data dictionary would show there are two virtual columns in this table
SELECT COLUMN_NAME, VIRTUAL_COLUMN FROM
USER_TAB_COLS WHERE TABLE_NAME = 'VIRTUAL_EG'
COLUMN_NAME VIR
------------------------------ ---
SALARY NO
ANUUAL_SALARY YES
COMMISION YES
So let me modify my insert statement:
SH@ORCL> INSERT INTO VIRTUAL_EG (SALARY)
SELECT ROWNUM * 100 FROM ALL_OBJECTS WHERE ROWNUM <=1000;
1000 rows created.
Let me select from the table:
select * from virtual_eg where rownum <=10;
SALARY ANUUAL_SALARY
---------- -------------
100 1200
200 2400
300 3600
400 4800
500 6000
600 7200
700 8400
800 9600
900 10800
1000 12000
We can create integrity constraints and indexes on the virtual columns:
CREATE INDEX IX_ANNUAL_SALARY ON VIRTUAL_EG(ANUUAL_SALARY)
One can add a virtual column to an existing table by using an alter statement:
ALTER TABLE VIRTUAL_EG ADD
( COMMISION AS (CASE WHEN SALARY < 5000 THEN 0.1 * SALARY
WHEN SALARY < 10000 THEN 0.15* SALARY
WHEN SALARY >= 10000 THEN 0.2 * SALARY
END ))
Data dictionary would show there are two virtual columns in this table
SELECT COLUMN_NAME, VIRTUAL_COLUMN FROM
USER_TAB_COLS WHERE TABLE_NAME = 'VIRTUAL_EG'
COLUMN_NAME VIR
------------------------------ ---
SALARY NO
ANUUAL_SALARY YES
COMMISION YES
Points to note for using virtual columns
1. An index defined using a virtual column is equalent to a function based index.
2. We cannot update a virtual column directly
3. Similarly we cannot use a virtual column in where clause of a delete statement
4. Query which refers a virtual column is eligible for result caching
Restrictions on using Virtual Columns
1. Virtual Columns can only be created in Relational Heap table
2. Virtual columns cannot refer to another virtual column by name
3. Any columns refered in column definition must be defined in the same table
4. It can refer to a deterministic user-defined function, but if it does, then you cannot use the virtual column as a partitioning key column.
5. The output of column_expression must be a scalar value.
6. Virtual columns cannot be used in materialised views or materialised view logs.
References:
For more information on Virtual columns check the below links:
No comments :
Post a Comment