今天在做一个变更时,遇到了以下错误:
ERROR 1138 (22004): Invalid use of NULL value
现在对情况进行还原:
创建表test1 :
mysql> create table test1 (id int,b varchar(10) DEFAULT null ,c varchar(10));
Query OK, 0 rows affected (0.02 sec)
插入值:
mysql> insert into test1 values(1,'a','a');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test1 values(1,'','a');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test1 values(1,null,'a');
Query OK, 1 row affected (0.01 sec)
对字段 b 进行更改:
mysql> ALTER TABLE test1 MODIFY COLUMN b varchar(10) NOT NULL DEFAULT '';
ERROR 1138 (22004): Invalid use of NULL value
将 b 字段中的null 值 改成 ' ' ,再次更改成功。
mysql> update test1 set b='' where b is null;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> ALTER TABLE test1 MODIFY COLUMN b varchar(10) NOT NULL DEFAULT '';
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0