Insert default value when null is inserted

后端 未结 5 1232
有刺的猬
有刺的猬 2021-02-13 21:07

I have an Oracle database, and a table with several not null columns, all with default values.

I would like to use one insert statement for any data I want to insert, a

5条回答
  •  不知归路
    2021-02-13 21:40

    I would like to use one insert statement for any data I want to insert, and don't bother to check if the values inserted are nulls or not.

    Define your table with a default value for that column. For example:

    create table myTable
    (
      created_date date default sysdate,
      ...
    )
    tablespace...
    

    or alter an existing table:

    alter table myTable modify(created_date default sysdate);
    

    Now you (or anyone using myTable) don't have to worry about default values, as it should be. Just know that for this example, the column is still nullable, so someone could explicitly insert a null. If this isn't desired, make the column not null as well.

    EDIT: Assuming the above is already done, you can use the DEFAULT keyword in your insert statement. I would avoid triggers for this.

提交回复
热议问题