“数字”列上的Oracle默认值

时间:2011-05-26 15:19:57

标签: oracle10g

我在名为SampleTable的表上有一个数字列,其定义如下

身份证号码(10)DEFAULT 1

我在不同的表(SomeTable)和UPDATE EACH ROW(Trigger - :New.ID)上有一个触发器,它将该表(SomeTable)中的值插入到SampleTable中。

有时来自SomeTable的ID可能为NULL,我希望SampleTable在我的情况下插入默认值1。

但是它在表上插入了NULL(Blankspace)。

我是PL / SQL的新手,所以非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

仅当插入中未指定列时才使用列默认值:

insert into sampletable (othercol) values ('x'); -- ID will default to 1 here

如果已指定列,则不使用它,即使已传入NULL:

insert into sampletable (othercol, id)
values ('x', null); -- ID will be set to null here

要解决此问题,您的触发器可以执行此操作:

insert into sampletable (othercol, id)
values ('x', coalesce(:new.id,1));