【MySQL】Out of range value for columns

Posted by 西维蜀黍 on 2020-04-25, Last Modified on 2021-11-08

Reproduce

drop table if exists test_tinyint;
create table test_tinyint (
    num tinyint
) engine=innodb charset=utf8;

insert into test_tinyint values(-100);
insert into test_tinyint values(255);

执行第 7 行的代码时候报错 "Out of range value for column ’num’ at row 7",这其实是因为(Signed 的) TINYINT 的范围是 -128 到 127,而 255 超过了这个范围。

如果把第 3 行的 num 字段定义改为 "num tinyint unsigned",第 7 行的插入就不会报错了。但是第 6 行的插入 - 100 又报错了,因为无符号整型是无法表示负数的。

Reference