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.

 

转载于:https://www.cnblogs.com/simonhaninmelbourne/archive/2013/01/23/2872441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值