mysql改变字段类型报错

mysql> ALTER TABLE user modify phone int(25) not null;
ERROR 1366 (HY000): Incorrect integer value: '' for column 'phone' at row 1

你之前的字段类型是什么?
字段类型不是随便可以改的,尤其是在有数据的情况下


我模拟出来了,你原表中存在有空字符串'',而不是null,因此修改会报错

create table test_20220404_a (b VARCHAR(25) not null);
insert into test_20220404_a values ('');
ALTER TABLE test_20220404_a modify b int(25) not null;

img

下面这个方式可以把你的这个字段成功改掉,但是由于你设置了不允许为null,因此只能默认设置个0上去了

ALTER TABLE test_20220404_a modify b VARCHAR(25) ;
update test_20220404_a set b=0 where b ='';
ALTER TABLE test_20220404_a modify b int(25) not null;

img

你把int(25) 变成int()试试?

看你的int
mysql中int最多11位。你要想设置25用varchar

书上写得就是int,