总结:虚拟列可以使用于一些特殊场合,实质是类似于函数列(即以 表中已有的列 经过函数运算得来),“虚拟列不存储在数据库中,是在执行查询时由oracle后台计算出来返回给用户”,因此虚拟列不会增加存储空间,但是由于需要计算,需要消耗额外的CPU Time。

---创建表时使用虚拟列

SQL> create table test4(id number,name varchar2(300),hash_id as(ora_hash(id)));

Table created

---alter 表新增虚拟列

SQL> create table test as select * from dba_objects;

Table created

SQL> alter table test add hash as (ora_hash(object_id));

Table altered

---below refer to https://oracleinstall.wordpress.com/2011/06/06/virtual-column-in-oracle-11g/

Virtual columns  allows users to create columns whose data is not supplied by the user, but it is derived by oracle server implicitly from other columns. Most importantly, their disk space consumption is NULL because their data is not stored in the table.

Key points about Virtual columns:

1)Data can not be inserted into virtual column

2) The virtual column and the columns to be used in the derivation of virtual column data must belong to the same table.

3)It can be used as normal columns without any restriction. It can be constrained, indexed, and can be used in DML or DDL statements. Note that it cannot be updated in UPDATE statement

4)We can partition the table based on virtual column

We can use below query to define virtual columns defined in the users schema.

SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, HIDDEN_COLUMN FROM USER_TAB_COLS WHERE VIRTUAL_COLUMN = ‘YES’;

5)we can not create virtual columns on Temporary tables, object types, clusters, External tables and Index Organized Tables

I tried to create table using following statment and used sysdate in the virtual column expression and got the following error.

Syntax

COLUMN [DATA TYPE] [GENERATED ALWAYS] AS (EXPRESSION) [VIRTUAL]

create table virtual_column (MEMBER_ID number(10), MEMBER_NAME varchar2(25), BIRTH_DATE date, AGE_IN_MONTHS number(5) AS(SYSDATE-BIRTH_DATE))

ORA-54002: only pure functions can be specified in a virtual column expression

create table virtual_column (MEMBER_ID number(10), MEMBER_NAME varchar2(25), MONTHLY_SALARY number(10,2), ANNUAL_SALARY number(10,2) AS(12*MONTHLY_SALARY))

SQL> insert into virtual_column values(1,’POOJITHA’,5000,10000); insert into ukatru.virtual_column values(1,’POOJITHA’,5000,10000) * ERROR at line 1: ORA-54013: INSERT operation disallowed on virtual columns

insert into virtual_column(MEMBER_ID,MEMBER_NAME,MONTHLY_SALARY) values(1,’POOJITHA’,5000);

SQL> select * from ukatru.virtual_column;

MEMBER_ID MEMBER_NAME               MONTHLY_SALARY ANNUAL_SALARY ———- ————————- ————– ————- 1 POOJITHA                            5000         60000

Create index on virtual columns:

CREATE INDEX IDX_ANUAL_SALARY ON virtual_column (ANNUAL_SALARY);

If you query user_indexes table the index  is created as function based index.

SELECT INDEX_NAME,  INDEX_TYPE, FUNCIDX_STATUS FROM   USER_INDEXES WHERE TABLE_NAME = ‘VIRTUAL_COLUMN’;

INDEX_NAME                     INDEX_TYPE                  FUNCIDX_ —————————— ————————— ——– IDX_ANUAL_SALARY               FUNCTION-BASED NORMAL       ENABLED

we can use alter table command to add virtual column to the table.

alter table virtual_column add QUARTERLY_SALARY AS (MONTHLY_SALARY*3)

Adding constraint on the virtual column.

ALTER TABLE virtual_column ADD CONSTRAINT QUARTERLY_SALARY_CHECK CHECK(QUARTERLY_SALARY != 0);

SQL> insert into virtual_column(MEMBER_ID,MEMBER_NAME,MONTHLY_SALARY) values(2,’POOJITHA’,0); insert into virtual_column(MEMBER_ID,MEMBER_NAME,MONTHLY_SALARY) values(2,’POOJITHA’,0) * ERROR at line 1: ORA-02290: check constraint (QUARTERLY_SALARY_CHECK) violated

[oracle 11g 新特性] virtual column虚拟列的更多相关文章

  1. Oracle 11g 新特性 --SQL Plan Management 说明

    Oracle 11g 新特性 --SQL Plan Management 说明 参见大神博主文章: http://blog.csdn.net/tianlesoftware/article/detail ...

  2. Oracle 11g 新特性 – HM(Hang Manager)简介

    在这篇文章中我们会对oracle 11g 新特性—hang 管理器(Hang Manager) 进行介绍.我们需要说明,HM 只在RAC 数据库中存在. 在我们诊断数据库问题的时候,经常会遇到一些数据 ...

  3. Oracle 11g新特性

    文章转自网络 Oracle 11g于2007年7月11日美国东部时间11时(北京时间11日22时)正式发布,11g是甲骨文公司30年来发布的最重要的数据库版本,根据用户的需求实现了信息生命周期管理(I ...

  4. 使用Oracle 11g新特性 Active Database Duplication 搭建Dataguard环境

    Duplication Database 介绍 Duplicate database可以按照用途分为2种: duplicate database(复制出一个数据库) duplicate standby ...

  5. Oracle 11g 新特性(一)-- 虚拟列

    数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Oracle11g 增加了虚拟列的新特性, 具体说明如 ...

  6. Oracle 11g新特性虚拟列分区

    如今有个需求:一个单据表要依照月份来分区.假设是在Oracle 10g上,仅仅能再加一个字段. 在Oracle 11g以后就不一样了.能够用虚拟列处理. SQL> select * from v ...

  7. Oracle 11g 新特性 -- 自适应游标共享(Adaptive Cursor Sharing: ACS) 说明(转载)

    一.自适应游标共享(Adaptive Cursor Sharing) 说明 1.1 ACS概述绑定变量使Oracle DB 可以为多条SQL 语句共享单个游标,以减少分析SQL 语句所使用的共享内存量 ...

  8. Oracle 11g新特性延迟段创建和truncate的增强

    下面测试Oracle 11g开始的新特性truncate的增强和延迟段空间创建. Oracle从11g开始,当用户创建一张空表的时候不会先分配段和空间,只有当对这张表插入第一行数据的时候才分配段和空间 ...

  9. Oracle 11g新特性 -- 延迟段

    11gR2之前的版本中,当创建一张表时,会自动分配段空间,这样做有几个弊端: 1. 初始创建表时就需要分配空间,自然会占用一些时间,如果初始化多张表,这种影响就被放大. 2. 如果很多表开始的一段时间 ...

随机推荐

  1. poj1691(dfs)

    链接 dfs了 写得有点乱 #include <iostream> #include<cstdio> #include<cstring> #include<a ...

  2. bzoj3611

    会构建虚树之后就是noip提高组的题目了稍微难一点的是求代价和,只要注意按一个方向避免重复计算贡献即可 ; type node=record po,next:longint; end; ..] of ...

  3. CI 在nginx中出现404错误的解决方式

    因为你的nginx配置的是截取.php文件后缀的访问转发到PHP-CGI,而index.php和index.php/是不一样的.. 你在nginx里面写一句: if (!-e $request_fil ...

  4. ES5新特性:理解 Array 中增强的 9 个 API

    为了更方便的对JS中Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.index ...

  5. table显示json数据传递

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. LittleTool之批量修改材质

    using UnityEngine; using System.Collections; using UnityEditor; public class ChangeMaterial : Editor ...

  7. HW5.10

    public class Solution { public static void main(String[] args) { int count = 0; for(int i = 1; i < ...

  8. HDOJ-ACM1019(JAVA) 多个数的最小公倍数

    题意:求多个数的最小公倍数 很简单,但是我一开始的做法,估计会让结果越界(超过int的最大值) import java.util.*; import java.io.*; public class M ...

  9. HDU3695 - Computer Virus on Planet Pandora(AC自动机)

    题目大意 给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以 题解 建立好自动机后.把文本串T正反各匹配一次,刚开始一直TLE...后面找到原因是重复的子串很多以及有模式 ...

  10. 如何生成一副Poker

    import java.util.LinkedList;import java.util.Random;//扑克类class Poker{    String color;//花色    String ...