How to compute a column value in oracle 10g?

前端 未结 2 1544
夕颜
夕颜 2021-01-15 00:18
create table ord_tbl
(
ord_id number(10) primary key,
ord_name varchar2(20),
quantity number(20),
cost_per_item number(30),
total_cost number(30)--This colm shud be          


        
2条回答
  •  情话喂你
    2021-01-15 01:03

    Like Gordon Linoff answered, you can create a view. Alternatively you can create a trigger and store the actual value:

    create trigger tiua_ord_tbl
    on ord_tbl after insert or update
    for each row
    begin
      :new.total_cost := :new.quantity * :new.cost_per_item;
    end;
    

    The advantage of storing the data, is that you can access it faster and index it if you need. The disadvantage is that you store redundant data (it can be calculated at runtime) and it requires more storage space.

    I would advise to use the view at first, and only start storing the value in the table if you need this 'cached' version for performance.

提交回复
热议问题