# 空值（SQL）

**空值**（Null，常写作 NULL）是 SQL 中一种特殊的标记，代表缺失或不适用的值：从逻辑上说，即“该字段的值未知”<sup>[1](https://www.duckdb.org/docs/current/sql/data_types/nulls)</sup>。在大多数 SQL 引擎中，空值与空字符串或零不同；在普通的相等比较下，任意两个空值都不相等<sup>[2](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/null-and-unknown-transact-sql?view=sql-server-ver16)</sup>。由于空值代表未知，涉及空值的比较不会返回真或假，而是返回第三种逻辑值——未知，这正是 SQL 三值逻辑的根源<sup>[3](https://modern-sql.com/concept/null)</sup>。如今，所有主流 DBMS 和数据文件格式都支持空值，且空值被广泛使用<sup>[4](https://dl.acm.org/doi/pdf/10.1145/3662010.3663452)</sup>。

| 关键事实 | 细节 |
|---|---|
| 空值的含义 | 表示未知或缺失值的标记；在大多数引擎中，它不同于零和空字符串<sup>[2](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/null-and-unknown-transact-sql?view=sql-server-ver16)</sup> |
| 比较规则 | `7 = NULL` 和 `7 <> NULL` 都得到空值（未知），而非真或假<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup> |
| 检测空值 | `IS NULL` 和 `IS NOT NULL` 总是返回真或假，绝不返回空值<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup> |
| 空值安全相等 | `IS NOT DISTINCT FROM` 将两个空值视为相等；MySQL 的 `<=>` 与之等价<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup><sup> • </sup><sup>[6](https://dev.mysql.com/doc/refman/26.7/en/comparison-operators.html)</sup> |
| 聚合函数 | 大多数聚合函数忽略空值输入；`count(*)` 统计所有行，而 `count(col)` 只统计非空行<sup>[7](https://www.postgresql.org/docs/18/sql-expressions.html)</sup> |
| 经典缺陷 | 子查询中含空值时，`NOT IN` 总是返回空结果<sup>[8](https://sqlperformance.com/2019/12/t-sql-queries/null-complexities-part-1)</sup> |
| 标准变更 | SQL:2023 将 `unique nulls [not] distinct` 增列为可选特性 F292<sup>[3](https://modern-sql.com/concept/null)</sup> |

## 历史与缘由

[Edgar F. Codd](https://www.edgepedia.org/zh/edgar-f-codd)（Edgar F. Codd）在关系模型中引入空值时，一并引入了三值逻辑，其第三个真值为“未知”，与真、假并列；涉及空值的比较求值为未知<sup>[9](https://www.inf.unibz.it/~franconi/papers/Franconi%20and%20Tessaris%20-%202012%20-%20The%20Algebra%20and%20the%20Logic%20for%20SQL%20Nulls.pdf)</sup>。20 世纪 70 年代，关于其语义的研究持续进行：1975 年的一篇论文开启了分析，1979 年的一篇论文则描述了空值在 SQL 算术与比较运算中经由三值逻辑传播的语义<sup>[4](https://dl.acm.org/doi/pdf/10.1145/3662010.3663452)</sup>。

这一设计自此便争议不断。C. J. Date 主张，使用空值所带来的三值逻辑会在 SQL 查询中产生歧义；2007 年《SIGMOD Record》上的一篇考察文章则认为，SQL 实际上对 Date 提出的查询给出了正确答案，只是 Date 认为自己问的是另一个问题，尽管如此，该文作者仍赞同 Date 关于 SQL 使用空值的总体结论<sup>[10](https://sigmodrecord.org/publications/sigmodRecord/0712/p13.article-rubinson.pdf)</sup>。2023 年的一篇 PODS 论文走得更远，它表明：与广泛持有的观点相反，SQL 本可以建立在二值逻辑之上而不损失任何表达力；在 2VL 语义下编写的查询可以高效地转换为 3VL SQL 并在任何现有 RDBMS 上运行；且 2VL 还能带来额外的优化<sup>[11](https://dl.acm.org/doi/10.1145/3584372.3588661)</sup>。

## 三值逻辑

当任一输入为空值时，普通比较运算符会得到空值，表示“未知”。例如，`7 = NULL` 得到空值，`7 <> NULL` 亦然<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup>。在 DuckDB 中同样如此：`NULL = NULL` 返回 `NULL` 而非真或假<sup>[12](https://www.duckdb.org/docs/current/sql/expressions/comparison_operators)</sup>。因为在普通 `=` 比较下，`null = null` 的结果是未知而非真，所以空值不等于任何东西，甚至不等于另一个空值<sup>[13](https://modern-sql.com/feature/is-distinct-from)</sup>。

布尔表达式按 Kleene 式规则传播未知：除非运算符的结果不依赖于未知操作数，否则结果即为未知。在 SQL Server 中，`TRUE AND UNKNOWN` 为未知，但 `FALSE AND UNKNOWN` 为假<sup>[2](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/null-and-unknown-transact-sql?view=sql-server-ver16)</sup>。Microsoft 的文档称这种对三值逻辑的需要是许多应用程序错误的根源<sup>[2](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/null-and-unknown-transact-sql?view=sql-server-ver16)</sup>。

算术运算同样传播空值：在 SQLite 兼容性矩阵所考察的所有引擎中，任何数加上空值都得到空值，空值乘以零也得到空值<sup>[14](https://www.sqlite.org/nulls.html)</sup>。因此 `30+NULL` 和 `NULL+NULL` 都返回空值<sup>[15](https://vldb.org/pvldb/vol15/p2613-guagliardo.pdf)</sup>。

**未知的去向。** WHERE 子句将未知当作假处理，因此 `col = null` 永远不会匹配任何行<sup>[3](https://modern-sql.com/concept/null)</sup>。CHECK 约束遵循不同的规则：只要约束条件求值不为假，约束即被满足，因此真和未知都被接受<sup>[9](https://www.inf.unibz.it/~franconi/papers/Franconi%20and%20Tessaris%20-%202012%20-%20The%20Algebra%20and%20the%20Logic%20for%20SQL%20Nulls.pdf)</sup>。

## 检测空值：谓词与空值安全相等

由于 `=` 无法检测空值，SQL 提供了 `IS NULL` 和 `IS NOT NULL` 谓词，它们总是返回真或假，绝不返回空值，即使操作数为空值也是如此<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup>。SQL Server 的文档正是建议在 WHERE 子句中这样使用<sup>[2](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/null-and-unknown-transact-sql?view=sql-server-ver16)</sup>。

对于行值表达式，这些谓词的行为令许多用户感到意外：在 [PostgreSQL](https://www.edgepedia.org/zh/postgresql) 中，当行本身为空值或其所有字段均为空值时，`IS NULL` 为真；只有当行及其所有字段都非空时，`IS NOT NULL` 才为真，因此一个既含空值字段又含非空字段的行，对这两个检测都返回假<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup>。

**空值安全相等。** 标准给出的答案是 `IS DISTINCT FROM` 谓词，它对非空输入的行为类似 `<>`，但当两个输入均为空值时返回假，仅当一个输入为空值时返回真；`IS NOT DISTINCT FROM` 是其反面<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup>。SQL Server 的文档直接展示了这种对比：对于 `NULL` 与 `NULL`，`A = B` 返回未知，而 `IS NOT DISTINCT FROM` 返回真；对于 `0` 与 `NULL`，同一对运算分别返回未知和假<sup>[16](https://learn.microsoft.com/en-us/sql/t-sql/queries/is-distinct-from-transact-sql?view=sql-server-ver17)</sup>。DuckDB 记录了相同的运算符，`NULL IS NOT DISTINCT FROM NULL` 返回真<sup>[12](https://www.duckdb.org/docs/current/sql/expressions/comparison_operators)</sup>。MySQL 将同样的语义写作空值安全等于运算符 `<=>`，当两个操作数均为空值时返回 1，当一个操作数为空值时返回 0，等价于标准的 `IS NOT DISTINCT FROM`<sup>[6](https://dev.mysql.com/doc/refman/26.7/en/comparison-operators.html)</sup>。

## 表达式与函数中的空值

若干函数用于替代空值或与空值比较。`COALESCE` 是标准的可变参数函数，返回第一个非空参数，且会短路求值；而 Oracle 的 `NVL` 无论何种情况都会对两个参数求值；`NULLIF` 通过将不需要的值（如 0）转为空值，来解决除零问题<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>。

使用数据表明哪些特性在实践中真正重要。在一项针对 SQL 用户的调查中，将近 70% 的受访者经常或定期使用 `IS NULL` 条件或其否定形式，完全避免使用的不到 10%；其次最常见的空值相关特性是 `COALESCE`，近半数受访者至少经常使用<sup>[15](https://vldb.org/pvldb/vol15/p2613-guagliardo.pdf)</sup>。

## 约束、键与排序中的空值

**键与约束。** SQL Server 不允许在需要值来区分行的位置（如主键或分布键）出现空值<sup>[2](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/null-and-unknown-transact-sql?view=sql-server-ver16)</sup>。唯一性约束 `UNIQUE(u1,...,un)` 仅当没有两行在所有唯一性列上以非空值完全匹配时才成立<sup>[9](https://www.inf.unibz.it/~franconi/papers/Franconi%20and%20Tessaris%20-%202012%20-%20The%20Algebra%20and%20the%20Logic%20for%20SQL%20Nulls.pdf)</sup>。由于两个空值不相等，唯一约束在 Oracle、PostgreSQL 和 MySQL 中允许任意数量的空值行，而 SQL Server 只允许一行<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>；SQLite 的跨引擎矩阵同样记录：在 SQLite、PostgreSQL、Oracle 和 OCELOT 中，UNIQUE 列中的空值被视为互不相同，而在 MS-SQL 中则不然<sup>[14](https://www.sqlite.org/nulls.html)</sup>。

**CHECK 约束。** 求值为未知的 CHECK 约束不算违反，因此 `salary` 为空值的行可以通过 `CHECK (salary > 0)`；若要求必须有值，则需要显式的 `NOT NULL`<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>。

**排序。** SQL 标准将 `ORDER BY` 中空值的默认位置留给厂商决定；SQL:2003 引入了 `NULLS FIRST|LAST` 修饰符（特性 T611），使开发者能够显式控制<sup>[3](https://modern-sql.com/concept/null)</sup>。默认位置各不相同：Oracle 和 PostgreSQL 默认将空值排在最后，而 MySQL 和 SQL Server 将空值排在最前<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>。MySQL 自己的文档确认，空值在 `ORDER BY ... ASC` 时排在最前，在 `DESC` 时排在最后<sup>[18](https://dev.mysql.com/doc/en/working-with-null.html)</sup>。Databricks 默认将空值排在最前，并在 `GROUP BY` 和 `DISTINCT` 处理中将空值归为一组，符合标准<sup>[19](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-null-semantics)</sup>。

## 数据解读

**聚合函数。** 大多数聚合函数忽略空值输入，丢弃表达式求值为空值的行<sup>[7](https://www.postgresql.org/docs/18/sql-expressions.html)</sup>。在此规则下，`count(*)` 返回输入行的总数，而 `count(f1)` 返回 `f1` 非空的行数，`count(distinct f1)` 统计互不相同的非空值<sup>[7](https://www.postgresql.org/docs/18/sql-expressions.html)</sup>。Databricks 以同样的例外陈述了同一规则：除 `COUNT(*)` 外，所有聚合函数都忽略空值<sup>[19](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-null-semantics)</sup>。`COUNT` 和 `regr_count` 从不返回空值；对于实际为空的集合，它们返回零<sup>[3](https://modern-sql.com/concept/null)</sup>，而 `MAX`、`MIN`、`SUM`、`AVG`、`EVERY`、`ANY` 和 `SOME` 在所有输入为空值或输入集为空时返回空值<sup>[19](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-null-semantics)</sup>。

标量算术与聚合函数之间的对比令用户困惑：SQL 对 `30+NULL` 和 `NULL+NULL` 返回空值，但 `SUM` 聚合对 `SUM(30,NULL)` 返回 30，对 `SUM(NULL,NULL)` 返回空值<sup>[15](https://vldb.org/pvldb/vol15/p2613-guagliardo.pdf)</sup>。

**用户行为。** 在同一项调查中，近 70% 的受访者定期或经常将列声明为 `NOT NULL`，略高于 5% 的受访者避免这种做法<sup>[15](https://vldb.org/pvldb/vol15/p2613-guagliardo.pdf)</sup>。当被问及语义本身时，约 20% 的受访者希望 SQL 将两个空值视为相等，而约 10% 的受访者希望它们不相等<sup>[15](https://vldb.org/pvldb/vol15/p2613-guagliardo.pdf)</sup>。

## 各 SQL 引擎的对比

最显著的差异是 Oracle 对空字符串的处理：Oracle 将空字符串视为空值，反之亦然，因此 `'' IS NULL` 为真，它是唯一这样做的主流数据库<sup>[3](https://modern-sql.com/concept/null)</sup><sup> • </sup><sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>。在其他地方，MySQL 允许向 `NOT NULL` 列插入零或空字符串，因为这些实际上并非空值；空值意味着没有值<sup>[18](https://dev.mysql.com/doc/en/working-with-null.html)</sup>。

其他差异由上文的各节推得：唯一约束在 Oracle、PostgreSQL 和 MySQL 中允许多个空值，但在 SQL Server 中只允许一个<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>；默认的空值排序将空值排在最后的是 Oracle 和 PostgreSQL，排在最前的是 MySQL 和 SQL Server，后两者缺乏显式的 `NULLS FIRST/LAST` 控制<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>；空值安全相等在 PostgreSQL、DuckDB 和 SQL Server 中写作 `IS NOT DISTINCT FROM`，而在 MySQL 中写作 `<=>`<sup>[5](https://www.postgresql.org/docs/18/functions-comparison.html)</sup><sup> • </sup><sup>[6](https://dev.mysql.com/doc/refman/26.7/en/comparison-operators.html)</sup><sup> • </sup><sup>[16](https://learn.microsoft.com/en-us/sql/t-sql/queries/is-distinct-from-transact-sql?view=sql-server-ver17)</sup>。SQLite 的矩阵并排列出了七个引擎的这些差异<sup>[14](https://www.sqlite.org/nulls.html)</sup>。

## 常见陷阱与防御性实践

**NOT IN 陷阱。** 一个常见的缺陷是：当 `NOT IN` 在 `WHERE` 子句中搭配的子查询在其值中返回空值时，没有任何行能满足该谓词，因为未知的否定永远不为真<sup>[8](https://sqlperformance.com/2019/12/t-sql-queries/null-complexities-part-1)</sup>。Databricks 记录了这一机制：当列表包含空值且不包含输入值时，`NOT IN` 总是返回未知<sup>[19](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-null-semantics)</sup>。MySQL 的 `IN()` 在此也遵循标准：不仅当左侧表达式为空值时返回空值，而且当未找到匹配且列表中某个表达式为空值时也返回空值<sup>[6](https://dev.mysql.com/doc/refman/26.7/en/comparison-operators.html)</sup>。

**其他陷阱。** 没有 ELSE 分支的 CASE 表达式在无分支匹配时返回空值<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>，任何对可能为空值的列进行意外比较，都会通过未知值悄悄过滤掉行<sup>[3](https://modern-sql.com/concept/null)</sup>。调查数据支持两种习惯：在需要值的地方将列声明为 `NOT NULL`（近 70% 的用户已定期或经常这样做<sup>[15](https://vldb.org/pvldb/vol15/p2613-guagliardo.pdf)</sup>），以及有意识地使用 `IS NULL`/`IS NOT NULL` 和 `COALESCE`，而非编写 `= NULL`<sup>[3](https://modern-sql.com/concept/null)</sup>。

**索引。** Oracle 不在 B 树索引中存储全为空值的键，因此诸如 `manager_id IS NULL` 的谓词无法使用单列索引，需要使用带常量的复合索引或基于函数的索引；PostgreSQL 会对空值建索引，并提供部分索引作为替代方案<sup>[17](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)</sup>。

## 近期发展

SQL:2023 标准将 `unique nulls [not] distinct` 增列为可选特性 F292“UNIQUE 空值处理”，允许约束指定空值是否被视为互不相同<sup>[3](https://modern-sql.com/concept/null)</sup>。在研究方面，2023 年 PODS 的成果——SQL 本可以建立在二值逻辑之上而不损失表达力，且 2VL 查询可高效转换为 3VL SQL——重新框定了自 Codd 最初设计以来持续已久的争论<sup>[11](https://dl.acm.org/doi/10.1145/3584372.3588661)</sup>。

## References

1. [DuckDB Documentation: NULL Values](https://www.duckdb.org/docs/current/sql/data_types/nulls)
2. [Microsoft Learn: NULL and UNKNOWN (Transact-SQL)](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/null-and-unknown-transact-sql?view=sql-server-ver16)
3. [Modern SQL: NULL](https://modern-sql.com/concept/null)
4. [NULLs! (ACM SIGMOD Record / SIGMOD 2024)](https://dl.acm.org/doi/pdf/10.1145/3662010.3663452)
5. [PostgreSQL Documentation: 9.2 Comparison Functions and Operators](https://www.postgresql.org/docs/18/functions-comparison.html)
6. [MySQL Reference Manual: Comparison Functions and Operators](https://dev.mysql.com/doc/refman/26.7/en/comparison-operators.html)
7. [PostgreSQL Documentation: 4.2 Value Expressions](https://www.postgresql.org/docs/18/sql-expressions.html)
8. [NULL complexities, Part 1 (SQLPerformance.com)](https://sqlperformance.com/2019/12/t-sql-queries/null-complexities-part-1)
9. [Franconi & Tessaris (2012). The Algebra and the Logic for SQL Nulls](https://www.inf.unibz.it/~franconi/papers/Franconi%20and%20Tessaris%20-%202012%20-%20The%20Algebra%20and%20the%20Logic%20for%20SQL%20Nulls.pdf)
10. [Nulls, Three-Valued Logic, and Ambiguity in SQL: Critiquing Date's Critique (SIGMOD Record, 2007)](https://sigmodrecord.org/publications/sigmodRecord/0712/p13.article-rubinson.pdf)
11. [SQL Nulls and Two-Valued Logic (SIGMOD-PODS 2023)](https://dl.acm.org/doi/10.1145/3584372.3588661)
12. [DuckDB Documentation: Comparison & Is-Not-Distinct-From Operators](https://www.duckdb.org/docs/current/sql/expressions/comparison_operators)
13. [Modern SQL: IS DISTINCT FROM](https://modern-sql.com/feature/is-distinct-from)
14. [NULL Handling in SQLite](https://www.sqlite.org/nulls.html)
15. [Troubles with Nulls, Views from the Users (PVLDB Vol. 15)](https://vldb.org/pvldb/vol15/p2613-guagliardo.pdf)
16. [Microsoft Learn: IS [NOT] DISTINCT FROM (Transact-SQL)](https://learn.microsoft.com/en-us/sql/t-sql/queries/is-distinct-from-transact-sql?view=sql-server-ver17)
17. [NULL Handling and Three-Valued Logic, Samer Al-Qaisi](https://sameralqaisi.me/databases/sql/null-handling-and-three-valued-logic/)
18. [MySQL Reference Manual: Working with NULL Values](https://dev.mysql.com/doc/en/working-with-null.html)
19. [Databricks SQL reference: NULL semantics](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-null-semantics)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › SQL and query languages › SQL language and syntax

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/null-sql

English original: https://www.edgepedia.org/null-sql
