Oracle11g新特性之--虚拟列(VirtualColumn)Oracle11G虚拟列VirtualColumn介绍Oracle11G在表中引入了虚拟列,虚拟列是一个表达式,在运行时计算,不存储在数据库

Oracle 11g新特性之--虚拟列(Virtual Column)


14:51:28 SCOTT@ test1 >CREATE TABLE EMP314:51:51 2 (14:51:51 3 EMPNO NUMBER(6),14:51:51 4 SAL NUMBER(8,2),14:51:51 5 COMM NUMBER(8,2),14:51:51 6 SAL_PACK GENERATED ALWAYS AS ( SAL + NVL(COMM,0) ) VIRTUAL14:51:51 7 )Table created.

2、查看虚拟列属性

14:56:10 SCOTT@ test1 >COL TABLE_NAME FOR A1014:56:19 SCOTT@ test1 >COL COLUMN_NAME FOR A2014:56:27 SCOTT@ test1 >COL DATA_TYPE FOR A2014:56:34 SCOTT@ test1 >COL DATA_DEFAULT FOR A2014:56:48 SCOTT@ test1 >R 1 select table_name,COLUMN_NAME,data_type,data_default,VIRTUAL_COLUMN from user_tab_cols 2* where table_name='EMP3'TABLE_NAME COLUMN_NAME DATA_TYPE DATA_DEFAULT VIR---------- -------------------- -------------------- -------------------- ---EMP3 SAL_PACK NUMBER "SAL"+NVL("COMM",0) YESEMP3 COMM NUMBER NOEMP3 SAL NUMBER NOEMP3 EMPNO NUMBER NO

上述建的虚拟列 SAL_PACK 是由一个简单的表达式创建的,使用的关键字有 VIRTUAL(不过这个关键字是可选的),该字段的值是由 COMM 这个字段通过表达式计算而来的。

在Table上添加虚拟列:

15:44:12 SCOTT@ test1 >alter table emp3 add (sal_total as (sal*12+comm) virtual);Table altered.15:49:11 SCOTT@ test1 >desc emp3; Name Null? Type ----------------------------------------------------------------- -------- -------------------------------------------- EMPNO NUMBER(6) SAL NUMBER(8,2) COMM NUMBER(8,2) SAL_PACK NOT NULL NUMBER SAL_TOTAL NUMBER 15:49:16 SCOTT@ test1 >select * from emp3; EMPNO SAL COMM SAL_PACK SAL_TOTAL---------- ---------- ---------- ---------- ---------- 10 1500 500 2000 18500 20 3000 500 3500 36500 30 4000 500 4500 48500 40 6000 500 6500 72500 15:51:00 SCOTT@ test1 >select table_name,COLUMN_NAME,data_type,data_default,VIRTUAL_COLUMN from user_tab_cols15:51:27 2 where table_name='EMP3';TABLE_NAME COLUMN_NAME DATA_TYPE DATA_DEFAULT VIR---------- -------------------- -------------------- -------------------- ---EMP3 SAL_TOTAL NUMBER "SAL"*12+"COMM" YESEMP3 SAL_PACK NUMBER "SAL"+NVL("COMM",0) YESEMP3 COMM NUMBER NOEMP3 SAL NUMBER NOEMP3 EMPNO NUMBER NO

09-15 00:19