Oracle SQL how to make database calculate a column?

72 views Asked by At

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?

1

There are 1 answers

0
Littlefoot On BEST ANSWER

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>