Unkown 等同于 PostgreSQL 中的 Null 吗?

Is Unkown equivalent to Null in PostgreSQL?

我在查询中进行了一些复杂的比较,我想确保在存在空值的情况下正确理解逻辑。似乎“未知”与 PostgreSQL 中的“空”是一回事,我没想到。

这是我的例子:

create table tab (col int);

insert into tab (col) values (7), (11), (null);


select col,
  (col = 7) is true as is_true,
  (col = 7) is false as is_false,
  (col = 7) is unknown as is_unknown,
  (col = 7) is null as is_null
  from tab;

它显示输出:

+-----+--------+---------+-----------+--------+
|a    |is_true |is_false |is_unknown |is_null |
+-----+--------+---------+-----------+--------+
|7    |true    |false    |false      |false   |
|11   |false   |true     |false      |false   |
|null |false   |false    |true       |**true**|
+-----+--------+---------+-----------+--------+

最后一个值不是应该是“false”而不是“true”吗?

问题:

a IS NULL时,下列表达式的结果是什么:

(a = 7) is null

Isn't the very last value supposed to be "false" instead of "true"?

来自SQL规范:

The data type boolean comprises the distinct truth values True and False. Unless prohibited by a NOT NULL constraint, the boolean data type also supports the truth value Unknown as the null value.

以下是从上面推导出来的,明确地:

答案:

没有。 a = 7,当a IS NULL,就是NULL。那么NULL IS NULL就是true.

这是 SQL 标准关于 NULL 值的说法:

Every data type includes a special value, called the null value, sometimes denoted by the keyword NULL. This value differs from other values in the following respects:

  • Since the null value is in every data type, the data type of the null value implied by the keyword NULL cannot be inferred; hence NULL can be used to denote the null value only in certain contexts, rather than everywhere that a literal is permitted.
  • Although the null value is neither equal to any other value nor not equal to any other value — it is unknown whether or not it is equal to any given value — in some contexts, multiple null values are treated together; for example, the <group by clause> treats all null values together.

这是标准对布尔值的规定:

The data type boolean comprises the distinct truth values True and False. Unless prohibited by a NOT NULL constraint, the boolean data type also supports the truth value Unknown as the null value. This specification does not make a distinction between the null value of the boolean data type and the truth value Unknown that is the result of an SQL <predicate>, <search condition>, or <boolean value expression>; they may be used interchangeably to mean exactly the same thing.

所以对于 booleanIS NULLIS UNKNOWN 相同。你可以说声明一个“未知”值有点愚蠢,如果你然后说它与 NULL 相同,但这是 SQL 标准的方式。

然而,当你考虑到有多少人正确理解 NULL 时,它会引发以下陈述,这感觉就像一个深刻的真理:

SELECT NULL IS UNKNOWN;

 ?column? 
══════════
 t
(1 row)