Set ORACLE table fields default value to a formular

后端 未结 2 1399
粉色の甜心
粉色の甜心 2021-01-15 05:20

I have an oracle table like this:

create table tms_transaction_tbl
(
trans_id number primary key,
location_id number,
trans_date date,
resource_id number,
ts         


        
2条回答
  •  Happy的楠姐
    2021-01-15 06:22

    You can't use a SELECT as a default value, it must be a constant.

    If you're using Oracle 11g this is what virtual columns are for. You can't insert into or update them but the provide a pre-calculated column in the database for you.

    create table tms_transaction_tbl
     ( trans_id number primary key,
       location_id number,
       trans_date date,
       resource_id number,
       ts_id number,
       max_value number,
       booked_units number default 0,
       remaining number generated always as ( max_value - booked_units ) virtual,
       booked number not null ,
       user_id number,
       trans_time timestamp
       );
    

    The syntax is further described in the documentation for the CREATE TABLE statement. If you're not using 11g you can achieve the same logic with a view on top of your table.

    If you don't want to use a view or a virtual column then I can only recommend that you do not store this data at all.

提交回复
热议问题