SQLite 中 NULL 值的处理及与其它数据库系统的比较
我们想让 SQLite 以兼容标准的方式处理 NULL。但在 SQL 标准里,NULL 到底该怎么处理并不明确,翻遍标准文档,也没法在所有情况下都看清它究竟要求怎么做。

所以除了参照标准文档,我们还测了不少流行的 SQL 引擎,看它们究竟怎么处理 NULL。思路很简单:让 SQLite 跟其他数据库引擎表现一致。有志愿者写了个测试脚本,在各种各样的关系型数据库上跑,专门看 NULL 值是怎么被处理的。我们最早在 2005 年 5 月做过一轮测试,文末附了这个脚本的副本。
最初 SQLite 遵循的编码标准是——下面表格里每个问题的答案都该是 YES。可一测其他 SQL 引擎,发现没一个是这样的。于是 SQLite 改成跟 Oracle、PostgreSQL 和 DB2 一致的方式处理 NULL。这么一来,在 SELECT DISTINCT、以及 SELECT 里的 UNION 操作符这类用来去重的操作中,NULL 会被当作彼此不同;但在有唯一约束的列上,NULL 又仍然算相同。这看着有点霸道,不过跟其他数据库引擎一比,这点霸道也就不算什么了。
当然,也可以让 SQLite 认为 SELECT DISTINCT 及 UNION 中的 NULL 是相同的。 这可以通过改变 sqliteInt.h 源文件中 NULL_ALWAYS_DISTINCT 的 #define 预理宏并重新编译来实现。
更新于 2003-07-13:当初写这篇文档时测过的那些数据库引擎,如今都更新了,所以大家很可能希望我把下面这张表也更新一下。最早的数据里,各家的行为非常不一致;不过时间一长,这些不一致大多都向 PostgreSQL/Oracle 模型靠拢了。唯一还算重要的差别是,Informix 和 MS-SQL 都认为 UNIQUE 列里的 NULL 彼此不同。
实际情况是,NULL 在 UNIQUE 约束上应当彼此相同,至于 SELECT DISTINCT 和 UNION 该怎么算,就不好说了。照理说,NULL 要么在任何地方都算相同,要么在任何地方都算不同。SQL 标准文档也倾向于认为 NULL 值处处相同。可直到写这篇文章的时候,还没有哪个 SQL 引擎在 SELECT DISTINCT 或 UNION 里把 NULL 当成相同的。
下表列出了对于 NULL 处理的试验结果。
| SQLite | PostgreSQL | Oracle | Informix | DB2 | MS-SQL | OCELOT | |
|---|---|---|---|---|---|---|---|
| NULL 加上任何值都得 NULL | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| NULL 乘 0 得 NULL | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| NULL 值在 UNIQUE 列中是彼此相同的 | Yes | Yes | Yes | No | (Note 4) | No | Yes |
| NULL 值在 SELECT DISTINCT 时是彼此相同的 | No | No | No | No | No | No | No |
| NULL 值在 UNION 中是彼此相同的 | No | No | No | No | No | No | No |
| "CASE WHEN null THEN 1 ELSE 0 END" 将会是 0? | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| "null OR true" 将会是 true | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| "not (null AND false)" 是 true | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
| MySQL 3.23.41 | MySQL 4.0.16 | Firebird | SQL Anywhere | Borland Interbase | |
|---|---|---|---|---|---|
| NULL 加上任何值都得 NULL | Yes | Yes | Yes | Yes | Yes |
| NULL 乘 0 得 NULL | Yes | Yes | Yes | Yes | Yes |
| NULL 值在 UNIQUE 列中是彼此相同的 | Yes | Yes | Yes | (Note 4) | (Note 4) |
| NULL 值在 SELECT DISTINCT 时是彼此相同的 | No | No | No (Note 1) | No | No |
| NULL 值在 UNION 中是彼此相同的 | (Note 3) | No | No (Note 1) | No | No |
| "CASE WHEN null THEN 1 ELSE 0 END" 将会是 0? | Yes | Yes | Yes | Yes | (Note 5) |
| "null OR true" 将会是 true | Yes | Yes | Yes | Yes | Yes |
| "not (null AND false)" 是 true | No | Yes | Yes | Yes | Yes |
| 注意: | 1. | 老版本的 firebird 会在 SELECT DISTINCT 及 UNION 在忽略所有的 NULL。 |
| 2. | 无测试数据。 | |
| 3. | MySQL version 3.23.41 不支持 UNION 。 | |
| 4. | DB2, SQL Anywhere, 以及 Borland Interbase 不能许 UNIQUE 列中有 NULL。 | |
| 5. | Borland Interbase 不支持 CASE 表达式。 |
以下脚本用于收集上面表中的数据。
-- I have about decided that SQL's treatment of NULLs is capricious and cannot be -- deduced by logic. It must be discovered by experiment. To that end, I have -- prepared the following script to test how various SQL databases deal with NULL. -- My aim is to use the information gather from this script to make SQLite as much -- like other databases as possible. -- -- If you could please run this script in your database engine and mail the results -- to me at drh@hwaci.com, that will be a big help. Please be sure to identify the -- database engine you use for this test. Thanks. -- -- If you have to change anything to get this script to run with your database -- engine, please send your revised script together with your results. -- -- Create a test table with data create table t1(a int, b int, c int); insert into t1 values(1,0,0); insert into t1 values(2,0,1); insert into t1 values(3,1,0); insert into t1 values(4,1,1); insert into t1 values(5,null,0); insert into t1 values(6,null,1); insert into t1 values(7,null,null); -- Check to see what CASE does with NULLs in its test expressions select a, case when b<>0 then 1 else 0 end from t1; select a+10, case when not b<>0 then 1 else 0 end from t1; select a+20, case when b<>0 and c<>0 then 1 else 0 end from t1; select a+30, case when not (b<>0 and c<>0) then 1 else 0 end from t1; select a+40, case when b<>0 or c<>0 then 1 else 0 end from t1; select a+50, case when not (b<>0 or c<>0) then 1 else 0 end from t1; select a+60, case b when c then 1 else 0 end from t1; select a+70, case c when b then 1 else 0 end from t1; -- What happens when you multiple a NULL by zero? select a+80, b*0 from t1; select a+90, b*c from t1; -- What happens to NULL for other operators? select a+100, b+c from t1; -- Test the treatment of aggregate operators select count(*), count(b), sum(b), avg(b), min(b), max(b) from t1; -- Check the behavior of NULLs in WHERE clauses select a+110 from t1 where b<10; select a+120 from t1 where not b>10; select a+130 from t1 where b<10 OR c=1; select a+140 from t1 where b<10 AND c=1; select a+150 from t1 where not (b<10 AND c=1); select a+160 from t1 where not (c=1 AND b<10); -- Check the behavior of NULLs in a DISTINCT query select distinct b from t1; -- Check the behavior of NULLs in a UNION query select b from t1 union select b from t1; -- Create a new table with a unique column. Check to see if NULLs are considered -- to be distinct. create table t2(a int, b int unique); insert into t2 values(1,1); insert into t2 values(2,null); insert into t2 values(3,null); select * from t2; drop table t1; drop table t2;





