如何在触发器中发现 NULL 值是否传递给可为空的列?
如何在 BEFORE INSERT 触发器中判断用户是否显式地为可为空的列传递了 NULL 值,或者用户是否根本没有为该列传递任何值。
换句话说,我将能够执行以下操作:
create table t (id, NUMBER, str VARCHAR2(30) );
create Trigger trg
befor INSERT on t
FOR earch row
DECLARE
BEGIN
IF :NEW.str is NULL
THEN
IF <NULL-Value was explicit passed> THEN
dbms_output.put_line( 'user has passed NULL');
ELSE
dbms_output.put_line( 'user has passed nothing');
END IF;
END IF;
END;
/
然后在
INSERT INTO t (id, str) VALUES (1, NULL);
我将看到
> user has passed NULL
之后,在
INSERT INTO t (id) VALUES (1);
我将看到之后
> user has passed nothing
How can one figure out in a BEFORE INSERT Trigger, whether the user has passed the NULL - value for an null-able column explicit, or whether the user hasn't pass any value at all for that column.
In other words, I will be able to do following:
create table t (id, NUMBER, str VARCHAR2(30) );
create Trigger trg
befor INSERT on t
FOR earch row
DECLARE
BEGIN
IF :NEW.str is NULL
THEN
IF <NULL-Value was explicit passed> THEN
dbms_output.put_line( 'user has passed NULL');
ELSE
dbms_output.put_line( 'user has passed nothing');
END IF;
END IF;
END;
/
and then after
INSERT INTO t (id, str) VALUES (1, NULL);
I will see
> user has passed NULL
and after
INSERT INTO t (id) VALUES (1);
I will see
> user has passed nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Oracle 中没有办法以编程方式区分故意传递的 NULL 和默认的 NULL。
抛开其他的不谈,当 NULL 仍然毫无意义时,它就会赋予 NULL 意义。你为什么要这么做?
@OneDayWhen 经济特区:
,对于数据库来说,“此页故意留空”。显然,Codd 在他生命的最后阶段已经识别了四种不同类型的 NULL。8-)
RDBMS 的其他风格区分空字符串和 NULL ,也许有些地方有一个约定,空字符串意味着“不适用”,而 NULL 没有任何意义,但是,Oracle 将空字符串视为 NULL,并且没有人实现 Codd 的 NULL2,所以我认为在 Oracle 中赋予 NULL 含义是危险的
。一些数据库理论家认为 NULL 是 Codd 的重大错误之一,对于不同类型的 NULL,这种错误会加倍。有关该主题的有趣替代方案请尝试阅读 Chris Date 的 如何在不使用 NULL 的情况下处理缺失信息
There is no way in Oracle to programmatically distinguish a NULL deliberately passed from a defaulted NULL.
Apart from anything else, it would ascribe meaning to NULL when NULL defiantly remains the absence of meaning. Why would you want to do that?
@OneDayWhen sez:
Yes, a "This page intentionally left blank" for databases. Apparently towards the end of his life Codd had identified four different types of NULL. 8-)
Other flavours of RDBMS distinguish between empty string and NULL, and perhaps some places have a convention that empty string means "inapplicable" and NULL means nothing. However, Oracle treats empty string as NULL and nobody has implemented Codd's NULL2. So I think it is dangerous to ascribe meaning to NULL in Oracle.
There are some database theorists who think NULL was one of Codd's big mistakes, and that goes double for different types of NULL. For an interesting alternative take on the topic try reading Chris Date's How To Handle Missing Information Without Using NULL
为什么要为此使用触发器而不是表列上的默认值?根据您对 APC 答案的评论,这不是您想要实现的目标吗?
Why would you want to use a trigger for this, rather than a default value on the table column? Based on your comments to APC's answer, isn't this what you're trying to achieve?