[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
[oracle 11g 新特性] virtual column虚拟列的更多相关文章
- Oracle 11g 新特性 --SQL Plan Management 说明
Oracle 11g 新特性 --SQL Plan Management 说明 参见大神博主文章: http://blog.csdn.net/tianlesoftware/article/detail ...
- Oracle 11g 新特性 – HM(Hang Manager)简介
在这篇文章中我们会对oracle 11g 新特性—hang 管理器(Hang Manager) 进行介绍.我们需要说明,HM 只在RAC 数据库中存在. 在我们诊断数据库问题的时候,经常会遇到一些数据 ...
- Oracle 11g新特性
文章转自网络 Oracle 11g于2007年7月11日美国东部时间11时(北京时间11日22时)正式发布,11g是甲骨文公司30年来发布的最重要的数据库版本,根据用户的需求实现了信息生命周期管理(I ...
- 使用Oracle 11g新特性 Active Database Duplication 搭建Dataguard环境
Duplication Database 介绍 Duplicate database可以按照用途分为2种: duplicate database(复制出一个数据库) duplicate standby ...
- Oracle 11g 新特性(一)-- 虚拟列
数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Oracle11g 增加了虚拟列的新特性, 具体说明如 ...
- Oracle 11g新特性虚拟列分区
如今有个需求:一个单据表要依照月份来分区.假设是在Oracle 10g上,仅仅能再加一个字段. 在Oracle 11g以后就不一样了.能够用虚拟列处理. SQL> select * from v ...
- Oracle 11g 新特性 -- 自适应游标共享(Adaptive Cursor Sharing: ACS) 说明(转载)
一.自适应游标共享(Adaptive Cursor Sharing) 说明 1.1 ACS概述绑定变量使Oracle DB 可以为多条SQL 语句共享单个游标,以减少分析SQL 语句所使用的共享内存量 ...
- Oracle 11g新特性延迟段创建和truncate的增强
下面测试Oracle 11g开始的新特性truncate的增强和延迟段空间创建. Oracle从11g开始,当用户创建一张空表的时候不会先分配段和空间,只有当对这张表插入第一行数据的时候才分配段和空间 ...
- Oracle 11g新特性 -- 延迟段
11gR2之前的版本中,当创建一张表时,会自动分配段空间,这样做有几个弊端: 1. 初始创建表时就需要分配空间,自然会占用一些时间,如果初始化多张表,这种影响就被放大. 2. 如果很多表开始的一段时间 ...
随机推荐
- Android开发之AlertDialog
http://www.cnblogs.com/Gaojiecai/archive/2011/12/10/2283156.html http://www.2cto.com/kf/201205/13187 ...
- sdut2536字母哥站队(dp)
简单DP 说是简单 还是推了好一会 推出来觉得好简单 保留当前i的最小值 dp[i] = min(dp[i],dp[j]+i-j-1) j<i #include <iostream> ...
- Journal.Today 1.0.0
我喜欢把一天的日记都放在一个文件中,而不是每次都新建一个文件. 写了一个脚本,检测当天是否已经新建日记,已经新建则打开,未新建则新建并打开. 其中我不太喜欢Wiz日记本来的标题命名方式,所以都 ...
- Axis2 WebService(基于REST风格)
http://www.lifeba.org/arch/java_axis2_webservice_rest.html Axis2除了提供传统的webservice方法外,还提供了对Rest的支持.Ax ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- 多控制器之UIWindow
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- oracle 临时表学习
临时表概念 临时表就是用来暂时保存临时数据(亦或叫中间数据)的一个数据库对象,它和普通表有些类似,然而又有很大区别.它只能存储在临时表空间,而非用户的表空间.ORACLE临时表是会话或事务级别的,只对 ...
- ACM2028
Lowest Common Multiple Plus Problem Description 求n个数的最小公倍数. input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n ...
- Codeforces149E - Martian Strings(KMP)
题目大意 给定一个字符串T,接下来有n个字符串,对于每个字符串S,判断是否存在T[a-b]+T[c-d]=S(1 ≤ a ≤ b < c ≤ d ≤ length(T)) 题解 对于每个字符串S ...
- 通过分析 JDK 源代码研究 Hash 存储机制
通过 HashMap.HashSet 的源代码分析其 Hash 存储机制 实际上,HashSet 和 HashMap 之间有很多相似之处,对于 HashSet 而言,系统采用 Hash 算法决定集合元 ...