Sunteți pe pagina 1din 13

Oracle 11g Virtual Columns

- Sagar T

What are virtual columns ?




Virtual columns are the columns, which derive their value during runtime through an expression.

Need of Virtual Column


Consider a scenario where you want to compute something based on the columns in your table. For e.g. calculating the salary package of employees based on salary and commission, calculating the price of an item after providing discount based on type of item, etc. Theres no syntax of creating a table pre- Oracle 11g which makes use of such computed column, which made you resort to using separate database views and triggers.

For e.g. using separate Database views




Calculating average of three no.s

CREATE TABLE virtual_need ( col_1 NUMBER, col_2 NUMBER, col_3 NUMBER);

INSERT INTO virtual_need VALUES(5,5,5); INSERT INTO virtual_need VALUES(5,10,15);

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

For e.g. using Trigger to compute value in derived column


You can add a column to the table CREATE TABLE virtual_need ( col_1 NUMBER, col_2 NUMBER, col_3 NUMBER, col_4 NUMBER); And create a BEFORE INSERT trigger on this table which will contain the logic/formula/expression to be computed on col_4

Limitations- Wastage of hard disk space

Introducing Virtual 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

Populating virtual columns


 

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

Indexing Virtual Columns


Virtual columns can have indexes imposed on them like any other non-virtual columns.
CREATE INDEX vct_idx ON VIRTUAL_COLUMN_TABLE (COL_3);
Note :The index created on virtual columns is always a function-based index. Oracle internally indexes the expression associated with the value*

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

* - SELECT * FROM USER_IND_EXPRESSIONS WHERE INDEX_NAME =VCT_IDX;

explain plan for SELECT * FROM VIRTUAL_COLUMN_TABLE where col_3 = 200; select * from table (dbms_xplan.display);

PLSQL functions with virtual columns


CREATE OR REPLACE FUNCTION get_product (in_num1 INTEGER, in_num2 NUMBER) RETURN NUMBER DETERMINISTIC AS BEGIN RETURN in_num1 * in_num2; END; Note : Only DETERMINISTIC functions can be used with virtual columns. CREATE TABLE VIRTUAL_COLUMN_TABLE2 (COL_1 INTEGER, COL_2 NUMBER, COL_3 GENERATED ALWAYS AS (get_product(COL_1,COL_2)) VIRTUAL ); BENEFITS : You can embed logic at table-level

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 !!

S-ar putea să vă placă și