一日一学Oracle --- Oracle virtual column(虚拟列)

本文深入探讨了Oracle数据库中虚拟列的功能与用法,包括如何利用虚拟列提高查询效率、创建索引与统计数据,以及在where子句中灵活运用虚拟列。通过实例演示了虚拟列的创建、修改、查询过程,并强调了虚拟列的限制条件。

1. 为什么要使用虚拟列

       (1)可以为虚拟列创建索引(oracle为其创建function index)

       (2)可以搜集虚拟列的统计信息statistics,为CBO提供一定的采样分析。

      (3)可以在where 后面使用虚拟列作为选择条件

      (4)只在一处定义,不存储多余数据,查询是动态生成。

2. 语法

   HR@bear> create table inv(
  2  inv_id   number,
  3  inv_count  number,
  4  inv_status  generated always as 
  5    case  when  inv_count <= 100 then 'GETTING LOW'
  6          when  inv_count > 100  then 'OKAY'
  7    end)
  8  );

其中 inv_status 为虚拟列

我们插入一条数据,然后再查询,可以看到虚拟列的值会根据定义动态生成。

HR@bear> insert into inv (inv_id, inv_count) values (1, 100);

1 row created.

HR@bear> select * from inv;

    INV_ID  INV_COUNT INV_STATUS
---------- ---------- -----------
         1        100 GETTING LOW


3.添加一个虚拟列

alter table inv add
inv_comm generated always as(inv_count * 0.1) virtual
);

 

4. 修改现有的一个虚拟列

alter table inv modify inv_status generated always as(
case when inv_count <= 50 then 'NEED MORE'
when inv_count >50 and inv_count <=200 then 'GETTING LOW'
when inv_count > 200 then 'OKAY'
end);

 

5.虚拟列可以在where子句中使用

SQL> update inv set inv_count=100 where inv_status='OKAY';

注意不能直接插入或修改虚拟列的值。

你可以定义虚拟列的数据类型,如果不指定,oracle会自动指定为定义中依赖的列的数据类型。

注意事项:

        (1) 只有堆组织表(heap-organized table)才可以定义虚拟列

        (2) 虚拟列不能引用其他的虚拟列

        (3) 虚拟列只能引用自己表中的列, 不能引用其他表中的列。

        (4) 虚拟列值只能是标量 scalar value (a single value, not a set of values)

 

一日一学Oracle。微笑


       

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值