空值-布尔表达式 [英] Null values - boolean expression
问题描述
所以我对考试作业有疑问,在这项作业中,我们有一堆布尔表达式,例如:
So I have a question in regards to an exam assignment, in this assignment we have a bunch of Boolean expressions like:
FALSE OR NULL = NULL
然后我们应该写布尔表达式的值.为此,我使用了三值逻辑,但是当您获得如下布尔表达式时,这将如何应用:
And then we are expected to write the value of the Boolean expression. To do this I am making use of the Three-valued logic, but how does that apply when you get a Boolean expression as follow:
(NULLL AND TRUE) OR FALSE
或
(NULL AND NULL) OR TRUE
第一个值很容易通过三值逻辑找到,但是我如何找出另外两个值.
The first one can easily be found through three-valued logic, but how do I figure out the other two.
我知道这是一个非常基本的问题,但是我对此仍然是陌生的.
Very basic question, I know, but I am still brand new to this.
预先感谢
推荐答案
您需要布尔值AND
和OR
的三相真值表:
You need three-phase truth tables for boolean AND
and OR
:
OPERAND1 OPERATOR OPERAND2 RESULT
------------------------------------
TRUE AND TRUE TRUE
TRUE AND FALSE FALSE
FALSE AND FALSE FALSE
TRUE AND NULL UNKNOWN
FALSE AND NULL FALSE
NULL AND NULL UNKNOWN
TRUE OR TRUE TRUE
TRUE OR FALSE TRUE
FALSE OR FALSE FALSE
TRUE OR NULL TRUE
FALSE OR NULL UNKNOWN
NULL OR NULL UNKNOWN
该表是缩写,依赖于布尔逻辑中的AND
和OR
的可交换属性.在大多数SQL变体中,UNKNOWN
和NULL
是等效的.用简单的英语来说,如果即使使用部分信息也可以知道该值,则会提供该值.例如,如果X
为true,则X OR Y
始终为 true ,Y是否已知以及其取值如何.类似地,如果X
为false,Y是否已知以及其取值如何,X AND Y
始终为 false .这些是布尔运算符上的引理,在介绍性集合论中以及其他地方,都有讲授.
This table is abbreviated, relying on the commutative properties of AND
and OR
in Boolean logic. In most SQL variants, UNKNOWN
and NULL
are equivalent. In plain English, if the value can be known even with partial information, it is provided. For example X OR Y
is always true if X
is true, whether or not Y is known and regardless of the value it takes. Similarly, X AND Y
is always false if X
is false, whether or not Y is known and regardless of the value it takes. These are lemmas on the Boolean operators and are taught in introductory set theory, among other places.
一旦有了这些真值表,就可以对表达式进行求值,并在不存在括号的情况下遵循SQL运算符的正确优先级顺序.
Once you have these truth tables, you can evaluate your expressions, following the proper precedence order for SQL operators when parentheses are not present.
以下是您的其中一个示例的评估链:
Here is an evaluation chain for one of your examples:
-
(NULL AND TRUE) OR FALSE
-
(NULL) OR FALSE
-
NULL OR FALSE
-
NULL
(NULL AND TRUE) OR FALSE
(NULL) OR FALSE
NULL OR FALSE
NULL
有关更多信息,请参见 Wikipedia的三值逻辑条目:SQL中的应用.
See Wikipedia's Three-valued logic entry:Application in SQL for more.
最后一点:NULL
永远不等于NULL
,因为您不知道两个未知值实际上是否相等.
One final note: NULL
is never equal to NULL
, because you cannot know if two unknown values are in fact equal to each other.
这篇关于空值-布尔表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!