`

Oracle 11g Virtual Column(原创)

 
阅读更多

Virtual Column
Oracle Database 11g lets you include a virtual column in a table. Unlike normal columns, a virtual column’s values aren’t inserted directly into a table. The virtual column you specify is always based on computing an expression or a function based on one or more other columns in the same table. Once you create a virtual column, you can query it just as you do any other column.
Virtual columns have the following important features:

  • You can index a virtual column.
  • You can use a virtual column in all types of DDL and DML statements.
  • The database doesn’t store the values of the virtual column on disk because these values are only computed on-the-fly when you reference the virtual column.
  • The datatype for a virtual column is optional. If you don’t explicitly specify the datatype, the virtual column will inherit the same datatype as the underlying expression.
  • You can collect optimizer statistics on a virtual column.
  • You can partition a table or an index on a virtual column.

You can create a virtual column either when you create a table, or later on, by using the alter table statement. There are two ways to create a virtual column. The first method, shown here, is to create the virtual column when you create the table:

SQL> create table  admin_emp (
         empno      NUMBER(5) PRIMARY KEY,
         ename      VARCHAR2(15) NOT NULL,
         job        VARCHAR2(10),
         mgr        NUMBER(5),
         hiredate   DATE DEFAULT (sysdate),
         photo      BLOB,
         SAL        number(7,2),
         hrly_rate  NUMBER(7,2) GENERATED ALWAYS AS (sal/2080),
         comm       NUMBER(7,2),
         deptno     NUMBER(3) NOT NULL
     );

The column HRLY_RATE is a virtual column. You must specify the generated always as clause when you create a virtual column. Actually, the generated always part of this clause is optional. The generated always clause tells us that the database doesn’t store the column values on disk, but rather, generates them only when a SQL statement refers to this virtual column. The last part of the clause (as) shows the expression the database uses to compute the values for the virtual column. In this example, the values of the HRLY_RATE column are generated from the SAL column, by computing the expression sal/2080. Because the SAL column provides the annual salary, the expression sal/2080 gives you the hourly salary for an employee.

The following restrictions apply to the creation of a virtual column:

  • You create a virtual column only on a heap table, which is the normal Oracle table. You can’t create a virtual column on an index-organized, temporary, external, object, or cluster table.
  • A virtual column can’t refer to another virtual column.
  • A virtual column can be built only on the columns from the same table as the virtual column is in.
  • The output of the virtual column must always be a scalar value.
  • The virtual column can’t be an Oracle-supplied datatype or a user-defined type, LOB, or LONG RAW type.

Note that you can’t directly update a virtual column. That is, the following statement would fail if we assume that HRLY_RATE is a virtual column:
SQL> update table employees
     set hrly_rate ...
You can, however, specify a virtual column in the where clause of an update statement. Similarly, you can specify a virtual column in the where clause of a delete statement.
The second way to create a virtual column is to do so after table creation, by using the alter table statement, as shown here:
SQL> alter table employees add (income AS (salary + (salary*commission_pct)));

Virtual Column-Based Partitioning

Once you create a table with one or more virtual columns, you can then employ the new virtual column-based partitioning scheme to partition that table.

SQL> CREATE TABLE users (
       id           NUMBER,
       username     VARCHAR2(20),
       first_letter VARCHAR2(1)
       GENERATED ALWAYS AS
     (
         UPPER(SUBSTR(TRIM(username), 1, 1))
     ) VIRTUAL
       )
     PARTITION BY LIST (first_letter)
     (
       PARTITION part_a_g VALUES ('A','B','C','D','E','F','G'),
       PARTITION part_h_n VALUES ('H','I','J','K','L','M','N'),
       PARTITION part_o_u VALUES ('O','P','Q','R','S','T','U'),
       PARTITION part_v_z VALUES ('V','W','X','Y','Z')
     )

enable row movement;
The following code inserts two rows into each partition defined in the table.
SQL> INSERT INTO users (id, username) VALUES (1, 'Andy Pandy');
SQL> INSERT INTO users (id, username) VALUES (1, 'Burty Basset');
SQL> INSERT INTO users (id, username) VALUES (1, 'Harry Hill');
SQL> INSERT INTO users (id, username) VALUES (1, 'Iggy Pop');
SQL> INSERT INTO users (id, username) VALUES (1, 'Oliver Hardy');
SQL> INSERT INTO users (id, username) VALUES (1, 'Peter Pervis');
SQL> INSERT INTO users (id, username) VALUES (1, 'Veruca Salt');
SQL> INSERT INTO users (id, username) VALUES (1, 'Wiley Cyote');
SQL> COMMIT;
SQL> EXEC DBMS_STATS.gather_table_stats(USER, 'USERS');
The following query shows the data was distributed as expected.
SQL> COLUMN table_name FORMAT A25
SQL> COLUMN partition_name FORMAT A20
SQL> COLUMN high_value FORMAT A40
SQL> SELECT table_name, partition_name, high_value, num_rows
       FROM user_tab_partitions
      ORDER BY table_name, partition_name;
     TABLE_NAME                PARTITION_NAME       HIGH_VALUE                                 NUM_ROWS
     ------------------------- -------------------- ---------------------------------------- ----------
     USERS                     PART_A_G             'A', 'B', 'C', 'D', 'E', 'F', 'G'                 2
     USERS                     PART_H_N             'H', 'I', 'J', 'K', 'L', 'M', 'N'                 2
     USERS                     PART_O_U             'O', 'P', 'Q', 'R', 'S', 'T', 'U'                 2
     USERS                     PART_V_Z             'V', 'W', 'X', 'Y', 'Z'                           2
     4 rows selected.

The last line of the code in the example shows that you can specify row movement when partitioning on a virtual column. When you enable row movement, if the virtual column’s value belongs to another partition, a row migrates to the appropriate partition from its current partition.

Note:if you don't enable row movement, when you update the partition key column, you'll get ORA-14402 error

 

参考至:《McGraw.Hill.OCP.Oracle.Database.11g.New.Features.for.Administrators.Exam.Guide.Apr.2008》

       http://www.oracle-base.com/articles/11g/partitioning-enhancements-11gr1.php

本文原创,转载请注明出处、作者

如有错误,欢迎指正

邮箱:czmcj@163.com

0
0
分享到:
评论

相关推荐

    Oracle11g中文文档.zip

    oracle11g官方中文帮助 Oracle 11g:ORACLE ACTIVE DATA GUARD.pdf Oracle Database 11g 高可用性.pdf Oracle 数据库 11g 中的分区.pdf Oracle 数据库 11g :真正应用测试与可管理性概述.pdf Oracle 数据库 11g...

    oracle11g官方中文文档完整版

    Oracle 11g 官方中文文档 包括一下部分文档: Oracle 11g:ORACLE ACTIVE DATA GUARD.pdf Oracle Database 11g 高可用性.pdf Oracle 数据库 11g :真正应用测试与可管理性概述.pdf Oracle 数据库 11g 中的分区....

    oracle11g驱动包

    oracle11g驱动包

    oracle11g安装步骤.

    oracle11g安装步骤.oracle11g安装步骤.oracle11g安装步骤.oracle11g安装步骤.

    oracle11g oci.dll文件

    Oracle 11g是一款甲骨文公司成功打造的功能强大的关系数据库管理系统,目前共发行了linux版本和windows版本,Oracle数据库的高效性、安全性、稳定性、延展性,是其成功的关键因素,世界上的几乎所有大型信息化系统都...

    oracle 11g 补丁

    oracle 11g p10350787_111070升级补丁oracle 11g p10350787_111070升级补丁

    Oracle11g日常操作与维护手册

    Oracle11g日常操作与维护手册\Oracle11g日常操作与维护手册\Oracle11g日常操作与维护手册\

    oracle11g自动建库

    oracle11g自动建库oracle11g自动建库oracle11g自动建库oracle11g自动建库oracle11g自动建库

    oracle11g官方文档中文版

    Oracle 11g 官方中文文档 包括: Oracle 11g:ORACLE ACTIVE DATA GUARD.pdf Oracle Database 11g 高可用性.pdf Oracle 数据库 11g :真正应用测试与可管理性概述.pdf Oracle 数据库 11g 中的分区.pdf Oracle 数据库...

    oracle 11g Linux64位安装包

    Linux版本 Oracle11g数据库 64位 完整安装包

    龙蜥anolis系统oracle11g安装包

    龙蜥anolis系统oracle11g安装包,内含安装脚本支持龙蜥系统下自动安装数据库,并还原数据库dmp文件

    Oracle11g客户端安装包.zip

    Windows环境使用exp,imp,expdp,impdp,sqldur2等导入导出数据命令的时候,需要安装Oracle11g客户端环境

    oracle驱动ojdbc678oracle11g

    ojdbc678,oracle11g驱动

    oracle 11g gateway

    oracle 11g gateway

    oracle11g 百度盘下载

    oracle11g 百度盘下载

    oracle11g安装部署手册

    Oracle11g在windows服务器部署记录文档,以及遇到的问题和解决方法

    oracle10G和oracle11G的OCI.dll

    oracle10G和11G的OCI.dll,主要用于navicat工具;oracle10G和11G的OCI.dll,主要用于navicat工具

    centos安装oracle11g

    在centos下安装oracle11g 使用yum源进行搭建 包括配置监听

Global site tag (gtag.js) - Google Analytics