How to compute a column value in oracle 10g?

前端 未结 2 1545
夕颜
夕颜 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:12

    10g doesn't have this feature. Instead, use a view:

    create table ord_tbl
    (
    ord_id number(10) primary key,
    ord_name varchar2(20),
    quantity number(20),
    cost_per_item number(30),
    ord_date date
    );
    
    create view vw_ord_tbl as
        select ord_id, ord_name, quantity, cost_perId, (quantity*cost_per_item) as total_cost, ord_date
        from ord_tbl;
    

    The alternative would be to have the column in the table to maintain the value using a trigger -- for both updates and inserts. I would suggest using the view, because maintaining the triggers adds a lot of maintenance overhead.

    EDIT (by Jason):

    In 11g you can create a virtual column in the table definition.

    create table ord_tbl (
        ord_id number(10) primary key,
        ord_name varchar2(20),
        quantity number(20),
        cost_per_item number(30),
        total_cost as (quantity*cost_per_item),
        ord_date date
    )
    

提交回复
热议问题