Friday, May 7, 2010

Oracle Database 11g New Feature (Virtual Column): Create a table with virtual Column

Create a table with virtual Column

create table marks (regno varchar2(10),

m1 number,
m2 number,
m3 number,
total number
generated always as
(nvl(m1,0) + nvl(m2,0) + nvl(m3,0))
virtual,
grade varchar2(30)
generated always as
(case When (nvl(m1,0) + nvl(m2,0) + nvl(m3,0)) > 290 Then 'A Grade'
When (nvl(m1,0) + nvl(m2,0) + nvl(m3,0)) >280 And (nvl(m1,0) + nvl(m2,0) + nvl(m3,0)) <= 290 Then 'B Grade'
Else 'C Grade'
end
) virtual
)

the column is specified as "generated always as", meaning the column values are generated at runtime, not stored as part of the table. the end of the column with "virtual"
insert into marks (regno,m1,m2,m3) values(101,90,89,88);
insert into marks (regno,m1,m2,m3) values(102,95,94,95);

SQL> select * from marks;

REGNO M1 M2 M3 TOTAL GRADE
---------- ---------- ---------- ---------- ---------- -------
101 90 89 88 267 C Grade
102 95 94 95 284 B Grade

No comments: