Function Based Virtual Columns in Oracle 11 g Database

Oracle Database 11g introduced new feature – virtual column. Instead storing data, the virtual column is calculated based on an expression stored in data dictionary. Let’s assume you have a table that stores customer information. The customer names are stored in two separate columns – first_name and last_name. The database queries often need to return the customer’s full name. Instead of storing the full name into the database or having to type it each time, you can define a new virtual column that is calculated, based on an expression: first_name || ' ' || last_name.

Create a Oracle Database Table with calculated virtual column:

CREATE TABLE customer(
   first_name VARCHAR2(32),
   last_name VARCHAR2(32),
   full_name AS (first_name || ' ' || last_name)
);

Add a new calculated virtual column to an existing Oracle Database Table:

ALTER TABLE customer
          ADD full_name AS (first_name || ' ' || last_name)

Some of the virtual column benefits include:

  • Automatic re-computation of derived columns for ad-hoc query tools.
  • Reduction in redundant disk space for columns that must be derived from other columns (e.g. a MONTH column that is derived from another DATE column).
  • Keep business logic in a single place.
  • Easier interval partitioning based on calculated columns.

You should keep in mind that:

  • virtual columns may not reference other virtual columns;
  • virtual columns only within the containing table. You cannot reference columns within other tables.

Oracle Database virtual columns has the nice side effect of assisting in streamlining partitioning. For example, assume that we have a table that is partitioned by year-month (i.e. 2007-07). With 11g virtual columns, instead of creating separate column, we can simply compute the partition key virtually, using a DATE column.

 

posted on 2013-01-23 07:51  Simon Han  阅读(218)  评论(0编辑  收藏  举报