超出Integer的范围值

时间:2009-10-28 12:55:19

标签: mysql

如果超过19位数,则Integer列类型的超出范围值不会截断为其最大值。 在以下示例中,第二行中不应该有-1值。这是一个错误吗?

drop table if exists test;

CREATE TABLE `test` (
  `col1` int(11) NOT NULL,
  `col2` int(11) NOT NULL,
  `col3` int(11) NOT NULL,
  `col4` int(11) NOT NULL,
  `col5` int(11) NOT NULL,
  `col6` int(11) NOT NULL,
  `col7` int(11) NOT NULL,
  `col8` int(11) NOT NULL,
  `lastupdate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `lastupdateid` varchar(100) NOT NULL
) ENGINE=InnoDB;


insert into test values('6022','2123456789012345678', '00','00','00','00','00','00', NULL, 'test');

mysql>select * from test;
+------+------------+------+------+------+------+------+------+---------------------+--------------+
| col1 | col2       | col3 | col4 | col5 | col6 | col7 | col8 | lastupdate          | lastupdateid |
+------+------------+------+------+------+------+------+------+---------------------+--------------+
| 6022 | 2147483647 |    0 |    0 |    0 |    0 |    0 |    0 | 2009-10-28 18:20:30 | test         | 
+------+------------+------+------+------+------+------+------+---------------------+--------------+
1 row in set (0.00 sec)


insert into test values('6022','21234567890123456789', '00','00','00','00','00','00', NULL, 'test');

mysql>select * from test;
+------+------------+------+------+------+------+------+------+---------------------+--------------+
| col1 | col2       | col3 | col4 | col5 | col6 | col7 | col8 | lastupdate          | lastupdateid |
+------+------------+------+------+------+------+------+------+---------------------+--------------+
| 6022 | 2147483647 |    0 |    0 |    0 |    0 |    0 |    0 | 2009-10-28 18:20:30 | test         | 
| 6022 |         -1 |    0 |    0 |    0 |    0 |    0 |    0 | 2009-10-28 18:20:34 | test         | 
+------+------------+------+------+------+------+------+------+---------------------+--------------+
2 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:4)

这不是一个错误,它只是integer overflow的一个案例。

如果您需要将数字字符串值设置为最大值,则应该在执行插入时添加代码,或者作为触发器。

有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html

您还可以尝试小选择(未经测试,我在这里没有MySQL访问权限):

SELECT CAST('2123456789012345678' AS INT(11))
SELECT CAST('21234567890123456789' AS INT(11))