This article presents new feature of 11g Virtual column.
Virtual column – a column that is not stored on disk. Value of the column is calculated by expression or function.
column_name [datatype] [GENERATED ALWAYS] AS (expression) [VIRTUAL]
GENERATED ALWAYS and VIRTUAL keywords are optional
Example:
--create deterministic function used by virtual column
CREATE OR REPLACE FUNCTION add_fnc(p_id NUMBER)
RETURN NUMBER
deterministic
IS
BEGIN
RETURN p_id + 3;
END;
/
CREATE TABLE test_tbl
(
static_col NUMBER,
--virtual column using simple syntax expression
vir_col1 NUMBER AS (static_col + 1),
--virtual column using long syntax expression
vir_col2 NUMBER generated always AS (static_col+2) virtual,
--virtual column long syntax with function
vir_col3 NUMBER generated always AS (add_fnc(static_col)) virtual
)
--partition by virtual columns
PARTITION BY RANGE(vir_col2)
subpartition BY hash(vir_col1) subpartitions 4
(PARTITION p1 VALUES less than(maxvalue));
--add primary key using virtual column
ALTER TABLE test_tbl add constraint test_tbl primary key(vir_col1); |
--create deterministic function used by virtual column
create or replace function add_fnc(p_id number)
return number
deterministic
is
begin
return p_id + 3;
end;
/
create table test_tbl
(
static_col number,
--virtual column using simple syntax expression
vir_col1 number as (static_col + 1),
--virtual column using long syntax expression
vir_col2 number generated always as (static_col+2) virtual,
--virtual column long syntax with function
vir_col3 number generated always as (add_fnc(static_col)) virtual
)
--partition by virtual columns
partition by range(vir_col2)
subpartition by hash(vir_col1) subpartitions 4
(partition p1 values less than(maxvalue));
--add primary key using virtual column
alter table test_tbl add constraint test_tbl primary key(vir_col1);
Continue reading →