可空列不表现如此

时间:2017-06-07 14:07:44

标签: mysql sql select null mariadb

我有一个表actor,其中包含可为空的列first_name。我将一个特定的行设置为null:

update actor set first_name=null where actor_id=201;
Query OK, 1 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

然后我尝试使用where子句中的null值查询该表,并且什么也得不回来:

select * from actor where first_name is null;
Empty set (0.00 sec)

但是,如果我选择检查该列是否为空字符串,则返回正确的值:

select * from actor where first_name = '';
[...]
1 row in set (0.00 sec)

存储引擎是MyISAM。为什么将空列设置为空字符串?

更新

我看到1警告,但mysql CLI没有显示任何警告。我怎么能这样做?

此外:

desc actor;
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| Field       | Type                 | Null | Key | Default           | Extra                       |
+-------------+----------------------+------+-----+-------------------+-----------------------------+
| actor_id    | smallint(5) unsigned | NO   | PRI | NULL              | auto_increment              |
| first_name  | varchar(45)          | NO   |     | NULL              |                             |
| last_name   | varchar(45)          | NO   | MUL | NULL              |                             |
| last_update | timestamp            | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+----------------------+------+-----+-------------------+-----------------------------+

1 个答案:

答案 0 :(得分:2)

从表描述看,包括first_name在内的所有列都不为空。你确定它是可以为空的列吗?