0% found this document useful (0 votes)
62 views13 pages

Oracle 11g Virtual Columns

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

| 


  

á 
What are virtual columns ?

ë Virtual columns are the columns, which


derive their value during runtime through
an expression.
eed 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.

There¶s 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 ISERT ITO virtual_need


( col_1  BER, VAL ES(5,5,5);
col_2  BER,
col_3  BER); ISERT ITO virtual_need
VAL ES(5,10,15);

sing 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_VAL E FRO
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 S _VAL E FRO virtual_need ;

Limitations- To display derived columns, you¶ll 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  BER,
col_2  BER,
col_3  BER,
col_4  BER);

And create a BEFORE ISERT 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)
ë anage the new "binary" XLType data

ë Virtual columns can be IDEXED.

ë Virtual columns can be used for partitioning of the table.


Syntax :
column_name [datatype] [GEERATED ALWAYS] AS [expression] [VIRT AL]

ë Parameters within [ ] are optional.

ë Based on the result of the expression, the datatype of the virtual column can
be decided
e.g.

CREATE TABLE VIRT AL_COL _TABLE


(COL_1 ITEGER,
COL_2  BER,
COL_3 GEERATED ALWAYS AS (COL_1 * COL_2) VIRT AL
);

COLUMN_NAME DATA_TYPE DATA_LENGTH DATA_DEFAULT VIRTUAL_COLUMN


1 COL_1  BER 22 O

2 COL_2  BER 22 O
3 COL_3  BER 22 "COL_1³ * "COL_2" YES
Populating virtual columns
ë Virtual columns can be used in the WHERE clause of PDATE and DELETE
statement but they can¶t be modified by DL.
ë You can OT explicitly write to a virtual column

ISERT ITO VIRT AL_COL _TABLE VAL ES (10,20,200)

Error report:
SQL Error: ORA-54013: ISERT operation disallowed on virtual columns

ë You need to specify column names to insert into a table having virtual columns.

ISERT ITO VIRT AL_COL _TABLE (COL_1,COL_2) VAL ES (10,20);


1 rows inserted

SELECT * FRO VIRT AL_COL _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 VIRT AL_COL _TABLE ADD COSTRAIT


vct_pk PRIARY KEY(COL_3);

SELECT ucc.column_name, uc.COSTRAIT_AE,


uc.COSTRAIT_TYPE, uc.TABLE_AE
FRO SER_COSTRAITS uc, SER_COS_COL S ucc
WHERE uc.TABLE_AE = 'VIRT AL_COL _TABLE'
AD CC.TABLE_AE = C.TABLE_AE
AD CC.COSTRAIT_AE = C.COSTRAIT_AE
Indexing Virtual Columns
Virtual columns can have indexes imposed on them like any other non-virtual
columns.
CREATE IDEX vct_idx O VIRT AL_COL _TABLE (COL_3);

ote :- The index created on virtual columns is always a function-based index. Oracle internally indexes
the expression associated with the value*

SELECT IDEX_AE, IDEX_TYPE, TABLE_AE, F CIDX_STAT S


FRO SER_IDEXES
WHERE TABLE_AE = 'VIRT AL_COL _TABLE';

IDEX_AE IDEX_TYPE TABLE_AE F CIDX_STAT S


VCT_IDX F CTIO-BASED ORAL VIRT AL_COL _TABLE EABLED

* - SELECT * FRO SER_ID_EXPRESSIOS WHERE IDEX_AE =µVCT_IDX¶;

explain plan
for SELECT * FRO VIRT AL_COL _TABLE where col_3 = 200;

select * from table (dbms_xplan.display);


PLSQL functions with virtual
columns
CREATE OR REPLACE F CTIO get_product (in_num1 ITEGER, in_num2
 BER)
RET R  BER DETERIISTIC
AS
BEGI
RET R in_num1 * in_num2;
ED;

ote : Only DETERIISTIC functions can be used with virtual columns.

CREATE TABLE VIRT AL_COL _TABLE2


(COL_1 ITEGER,
COL_2  BER,
COL_3 GEERATED ALWAYS AS (get_product(COL_1,COL_2)) VIRT AL
);

BEEFITS : You can embed logic at table-level


Limitations
ë Virtual column can only be of scalar datatype or
XLDATATYE. It can¶t be a user defined type, LOB or
RAW
ë Columns mentioned as part of the virtual column
expression should belong to the same table
ë o DLs are allowed on the virtual columns.
ë Expression can¶t reference any other virtual column
ë Virtual columns can only be created on ordinary
tables. ot allowed on index-organized, external,
object, cluster or temporary tables
ë Virtual column can¶t be used as a partitioning key for
virtual column-based partitioning, if deterministic
function is used as virtual column expression.
|   

  



You might also like