Search code examples
oraclederived

insert into derived column with in same table


i created this test table db and added 3 columns col,col2 and cal3 . I want the cal 3 value to be derived from col + col2 . Doesn't let be to insert data using insert satament. what is the sql statement to insert values.

The table created statement is as follows :-

create table db
 (
 col number(10),col2 number(20), cal3 number(10) generated always as (col+col2)
 );

Solution

  • Since you have a computed column you have to specify the columns you insert into:

    insert into db (col, col2) values (10, 20);
    

    a select * from db after the insert above would give you:

    | COL | COL2 | CAL3 |
    |-----|------|------|
    |  10 |   20 |   30 |