I have the table of 3 columns A, B, C. I want to store the sum of columns A B in the C column without using the DML statements. Can anyone help please how to do. ?

I have the table of 3 columns A, B, C. I want to store the sum of columns A B in the C column without using the DML statements. Can anyone help please how to do. ?

11.1 and especially you have virtual column

SQL> create table t
  2  (
  3     a number
  4   , b number
  5   , c generated always as (a+b) virtual
  6  );

Table created.

SQL> insert into t (a, b) values (1, 2);

1 row created.

SQL> select * from t;

         A          B          C
---------- ---------- ----------
         1          2          3

Before that, a front insert - trigger

SQL> create table t
  2  (
  3     a number
  4   , b number
  5   , c number
  6  );

Table created.

SQL> create or replace trigger t_default before insert on t for each row
  2  begin
  3    :new.c := :new.a+:new.b;
  4  end;
  5  /

Trigger created.

SQL> insert into t (a, b) values (1, 2);

1 row created.

SQL> select * from t;

         A          B          C
---------- ---------- ----------
         1          2          3

Tags: Database

Similar Questions

Maybe you are looking for