虚拟列表示那列的数据不是存储在表中的,只有当虚拟列的值被查询时,该值被计算出来。
语法:
column_name [datatype] [generated always] as (expression) [virtual]
eg: salary as (round(salary*(1+commission/100),2))
salary number generated always as (round(salary*(1+commission/100),2))
generated always子句意味着,该列的值是在运行时基于它所引用的列值产生的。
create table sales(
prod_id number(6) not null,
cust_id number not null,
time_id date not null,
channel_id char(1) not null,
quantity_sold number(3) not null,
amount_sold number(10,2) not null,
total_amount as (quantity_sold*amount_sold) );
create index test_virtual_index1 on sales(total_amount);
注:在虚拟列上创建的索引,是基于函数的普通索引。
select a.index_name,a.index_type,b.column_expression
from user_indexes a
inner join user_ind_expressions b
on a.index_name=b.index_name
where a.index_name='TEST_VIRTUAL_INDEX1';
INDEX_NAME INDEX_TYPE COLUMN_EXPRESSION
------------------------------ ------------------------------ ----------------------------------------
TEST_VIRTUAL_INDEX1 FUNCTION-BASED NORMAL "QUANTITY_SOLD"*"AMOUNT_SOLD"
可以从外键约束中引用虚拟列。只要在子表中创建一个外键,让它引用sales表中的虚拟列amount_sold即可。在表达式中使用的函数必须是确定性的,但是可以在创建索引后重新把函数编译为不确定性的,而不必使虚拟列无效。在使用虚拟列作为分区键时,可能需要这么做,因为在这种情况下,通常不能使用确定性函数作为虚拟列表达式。重新编译了函数以后,就必须重建虚拟列桑的索引。此外,如果在虚拟列上有任何约束,那么必须首先禁用这些约束,然后重新启它们。最后,必须重新收集表的统计信息