Oracle 11g Virtual Columns
Oracle 11g Virtual Columns
- Sagar T
Virtual columns are the columns, which derive their value during runtime through an expression.
Using Database views :CREATE OR REPLACE VIEW virtual_need_view AS SELECT col_1, col_2 , col_3 , (col_1 + col_2 + col_3 )/3 AS AVERAGE_VALUE FROM virtual_need If say, instead of average, only sum needs to be computed, you just need to alter the formula of the view. CREATE OR REPLACE VIEW virtual_need_view AS SELECT col_1, col_2 , col_3 , (col_1 + col_2 + col_3 ) AS SUM_VALUE FROM virtual_need ; Limitations- To display derived columns, youll always need to use views. View attributes are not physically stored in the database. View attributes cannot be indexed and cannot enjoy the rights of a table column You cannot collect statistics/ histograms on such computed columns
Oracle 11g introduced Virtual columns to handle such computed columns in a more convenient way. In Oracle 11g, a table can contain a virtual column which derives the values by evaluating expressions that might use:
Columns from the same table Constants Function calls (user-defined functions or SQL functions)
On the fly calculation of values, hence no storage consumption. Virtual columns might be used to
Eliminate some views Control table partitioning (DBA stuff) Manage the new "binary" XMLType data
Virtual columns can be INDEXED. Virtual columns can be used for partitioning of the table.
Syntax :
column_name [datatype] [GENERATED ALWAYS] AS [expression] [VIRTUAL]
Parameters within [ ] are optional. Based on the result of the expression, the datatype of the virtual column can be decided
e.g.
CREATE TABLE VIRTUAL_COLUMN_TABLE (COL_1 INTEGER, COL_2 NUMBER, COL_3 GENERATED ALWAYS AS (COL_1 * COL_2) VIRTUAL );
COLUMN_NAME 1 2 3 COL_1 COL_2 COL_3 DATA_TYPE NUMBER NUMBER NUMBER DATA_LENGTH 22 22 22 "COL_1 * "COL_2" DATA_DEFAULT VIRTUAL_COLUMN NO NO YES
Virtual columns can be used in the WHERE clause of UPDATE and DELETE statement but they cant be modified by DML. You can NOT explicitly write to a virtual column
INSERT INTO VIRTUAL_COLUMN_TABLE VALUES (10,20,200) Error report: SQL Error: ORA-54013: INSERT operation disallowed on virtual columns
You need to specify column names to insert into a table having virtual columns.
INSERT INTO VIRTUAL_COLUMN_TABLE (COL_1,COL_2) VALUES (10,20); 1 rows inserted SELECT * FROM VIRTUAL_COLUMN_TABLE ; COL_1 COL_2 COL_3 ---------------------- ---------------------- ---------------------10 20 200
Adding constraints
Virtual columns can hold all type of constraints which a normal column can. ALTER TABLE VIRTUAL_COLUMN_TABLE ADD CONSTRAINT vct_pk PRIMARY KEY(COL_3); SELECT ucc.column_name, uc.CONSTRAINT_NAME, uc.CONSTRAINT_TYPE, uc.TABLE_NAME FROM USER_CONSTRAINTS uc, USER_CONS_COLUMNS ucc WHERE uc.TABLE_NAME = 'VIRTUAL_COLUMN_TABLE' AND UCC.TABLE_NAME = UC.TABLE_NAME AND UCC.CONSTRAINT_NAME = UC.CONSTRAINT_NAME
SELECT INDEX_NAME, INDEX_TYPE, TABLE_NAME, FUNCIDX_STATUS FROM USER_INDEXES WHERE TABLE_NAME = 'VIRTUAL_COLUMN_TABLE';
INDEX_NAME VCT_IDX INDEX_TYPE FUNCTION-BASED NORMAL TABLE_NAME VIRTUAL_COLUMN_TABLE FUNCIDX_STATUS ENABLED
explain plan for SELECT * FROM VIRTUAL_COLUMN_TABLE where col_3 = 200; select * from table (dbms_xplan.display);
Limitations
Virtual column can only be of scalar datatype or XMLDATATYE. It cant be a user defined type, LOB or RAW Columns mentioned as part of the virtual column expression should belong to the same table No DMLs are allowed on the virtual columns. Expression cant reference any other virtual column Virtual columns can only be created on ordinary tables. Not allowed on index-organized, external, object, cluster or temporary tables Virtual column cant be used as a partitioning key for virtual column-based partitioning, if deterministic function is used as virtual column expression.
Oracle Errors Virtual Column
Thank You !!