【发布时间】:2020-12-01 10:59:01
【问题描述】:
我有一个表,其中有 15 列,而 proc 只填充 7 列。现在已要求将 NULL 插入所有其他列。无法更改列定义。除了在插入语句中将每个列和值指定为 NULL 之外,还有什么方法可以放置 NULL。数据库是oracle 12
【问题讨论】:
标签: sql oracle sql-insert
我有一个表,其中有 15 列,而 proc 只填充 7 列。现在已要求将 NULL 插入所有其他列。无法更改列定义。除了在插入语句中将每个列和值指定为 NULL 之外,还有什么方法可以放置 NULL。数据库是oracle 12
【问题讨论】:
标签: sql oracle sql-insert
当然。您只需要枚举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 ...。
【讨论】: