以MySQL5版本,int类型为例:
root@localhost(test2)14:46>create table test2 (a int(10) UNSIGNED); Query OK, 0 rows affected (0.12 sec)
root@localhost(test2)14:56>insert test2 values (10); Query OK, 1 row affected (0.00 sec)
root@localhost(test2)14:56>update test2 set a=a-11; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1
root@localhost(test2)14:57>show warnings; +---------+------+--------------------------------------------+ | Level | Code | Message | +---------+------+--------------------------------------------+ | Warning | 1264 | Out of range value for column 'a' at row 1 | +---------+------+--------------------------------------------+ 1 row in set (0.00 sec)
root@localhost(test2)14:57>select * from test2; +------------+ | a | +------------+ | 4294967295 | +------------+ 1 row in set (0.00 sec)
root@localhost(test2)14:59>delete from test2; Query OK, 1 row affected (0.00 sec)
root@localhost(test2)14:59>insert test2 values (-1); Query OK, 1 row affected, 1 warning (0.00 sec)
root@localhost(test2)14:59>show warnings; +---------+------+--------------------------------------------+ | Level | Code | Message | +---------+------+--------------------------------------------+ | Warning | 1264 | Out of range value for column 'a' at row 1 | +---------+------+--------------------------------------------+ 1 row in set (0.00 sec)
root@localhost(test2)14:59>select * from test2; +------+ | a | +------+ | 0 | +------+ 1 row in set (0.00 sec)
int占用4个字节,而int又分为无符号型和有符号性。对于无符号型的范围是0 到 4294967295;有符号型的范围是-2147483648 到 2147483647。 举一反三,其他类型都可能有类似问题,均需要考量。
可以通过sql_mode参数控制,但一般建议程序控制,比如:对表单项的值进行校验。