zoukankan      html  css  js  c++  java
  • [oracle 11g 新特性] virtual column虚拟列

    总结:虚拟列可以使用于一些特殊场合,实质是类似于函数列(即以 表中已有的列 经过函数运算得来),“虚拟列不存储在数据库中,是在执行查询时由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

    ############################## 通往精神的路很多,物质只是其中一种 ##############################
    http://www.onejava.com/article/oracle/wip/wiptop.htm
    https://docs.oracle.com/cd/A60725_05/html/comnls/us/index.htm
    http://www.oracle.com/technetwork/cn/developer-tools/apex/getting-started-094884-zhs.html
    https://docs.oracle.com/cd/B34956_01/current/html/docset.html
  • 相关阅读:
    Python 常用Web框架的比较
    数据库SQL优化大总结之 百万级数据库优化方案
    百万级数据下的mysql深度解析
    微信小程序:bindtap等事件传参
    微信小程序:POST请求data数据请求不到
    动软代码生成器分页存储过程
    微信 获取wx.config 参数 基类
    小程序中的block
    提高商城系统响应速度
    时光煮雨 Unity3D让物体动起来③—UGUI DoTween&Unity Native2D实现
  • 原文地址:https://www.cnblogs.com/pompeii2008/p/5437552.html
Copyright © 2011-2022 走看看