Insert default value when null is inserted

后端 未结 5 1223
有刺的猬
有刺的猬 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:19

    The better option for performance is the first one.

    Anyway, as I understand, you don't want to repeat the insert column names and values due the difficult to make modifications. Another option you can use is to run an insert with returning clause followed by an update:

    INSERT INTO schema.my_table
        ( pk_column, other_column, not_null_column_with_default_value)
    VALUES
        (:pk_column,:other_column, :not_null_column_with_default_value)
    RETURNING not_null_column_with_default_value 
    INTO :insered_value
    

    It seems to work with PHP.

    After this you can check for null on insered_value bind variable. If it's null you can run the following update:

    UPDATE my_table
       SET not_null_column_with_default_value  = DEFAULT
     WHERE  pk_column = :pk_column: 
    

提交回复
热议问题