Ozgeozcn
Ozgeozcn

Reputation: 9

Oracle SQL how to make database calculate a column?

I want to make database calculate a column. For example: There is a column named 'price'. every time I insert a new value into the price, I want another column named 'percent' automatically calculate 1% of the new value. Like this;

Price     Percent
100        1
250        2,5

How can I create this?

Upvotes: 0

Views: 79

Answers (1)

Littlefoot
Littlefoot

Reputation: 142713

Create a virtual column:

SQL> create table test
  2    (price       number,
  3     percent     number as (price * 0.01)        --> this
  4    );

Table created.

SQL> insert into test(price)
  2  select 100 from dual union all
  3  select 250 from dual;

2 rows created.

SQL> select * From test;

     PRICE    PERCENT
---------- ----------
       100          1
       250        2,5

SQL>

Upvotes: 3

Related Questions