為什么要使用虛擬列
(1)可以為虛擬列創建索引(Oracle為其創建function index)
(2)可以搜集虛擬列的統計信息statistics,為CBO提供一定的采樣分析。
(3)可以在where 后面使用虛擬列作為選擇條件
(4)只在一處定義,不存儲多余數據,查詢是動態生成。
語法
1 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 為虛擬列
我們插入一條數據,然后再查詢,可以看到虛擬列的值會根據定義動態生成。
添加一個虛擬列
alter table inv add inv_comm generated always as(inv_count * 0.1) virtual;
修改現有的一個虛擬列
1 alter table inv modify inv_status generated always as( 2 case when inv_count <= 50 then 'NEED MORE'
3 when inv_count >50 and inv_count <=200 then 'GETTING LOW'
4 when inv_count > 200 then 'OKAY'
5 end);
虛擬列可以在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)
參考文檔
http://blog.csdn.net/wangke8476/article/details/7032597