如何在触发器中发现 NULL 值是否传递给可为空的列?

发布于 2024-11-08 04:48:39 字数 737 浏览 0 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍暮颜 2024-11-15 04:48:39

Oracle 中没有办法以编程方式区分故意传递的 NULL 和默认的 NULL。

抛开其他的不谈,当 NULL 仍然毫无意义时,它就会赋予 NULL 意义。你为什么要这么做?


@OneDayWhen 经济特区:

“Codd本人提出了第二种类型
NULL 表示“不适用””

,对于数据库来说,“此页故意留空”。显然,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:

"Codd himself proposed a second type
of NULL to mean 'inapplicable'"

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

一杆小烟枪 2024-11-15 04:48:39

为什么要为此使用触发器而不是表列上的默认值?根据您对 APC 答案的评论,这不是您想要实现的目标吗?

create table t(id number, str varchar2(32) default 'default value');
create view v as select * from t;

insert into t(id) values (1);
insert into t(id, str) values (2,null);

insert into v(id) values (3);
insert into v(id, str) values (4,null);

select * from t;

ID                     STR                              
---------------------- -------------------------------- 
1                      default value                    
2                                                       
3                      default value                    
4                   

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?

create table t(id number, str varchar2(32) default 'default value');
create view v as select * from t;

insert into t(id) values (1);
insert into t(id, str) values (2,null);

insert into v(id) values (3);
insert into v(id, str) values (4,null);

select * from t;

ID                     STR                              
---------------------- -------------------------------- 
1                      default value                    
2                                                       
3                      default value                    
4                   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文