【问题标题】:How to insert null to certain columns in oracleoracle中如何将null插入到某些列
【发布时间】:2020-12-01 10:59:01
【问题描述】:

我有一个表,其中有 15 列,而 proc 只填充 7 列。现在已要求将 NULL 插入所有其他列。无法更改列定义。除了在插入语句中将每个列和值指定为 NULL 之外,还有什么方法可以放置 NULL。数据库是oracle 12

【问题讨论】:

    标签: sql oracle sql-insert


    【解决方案1】:

    当然。您只需要枚举insert 语句中的目标列,不包括那些您不想为其提供值的列。 Oracle 将为每一列分配默认值(如果没有默认值,则为null)。

    假设你有下表:

    create table mytable (
        col1 int not null primary key, 
        col2 int not null, 
        col3 int default 0,  -- has a default 
        col4 int,            -- has no default
        col5 int
    );
    

    你可以这样做:

    insert into mytable (col1, col2)
    values (1, 2);
    

    你会得到:

    COL1 | COL2 | COL3 | COL4 | COL5 ---: | ---: | ---: | ---: | ---: 1 | 2 | 0 | |

    当然,如果任何列被定义为not null 并且没有默认值,这将失败并出现错误ORA-01400: cannot insert NULL into ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2012-06-20
      • 2023-03-21
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多