整数值都是完全一样的。对于SIGNED
或UNSIGNED
,它们都在-128至127的范围内。正如其他答案所指出的,括号中的数字仅仅是一个显示宽度提示。
但是,您可能需要注意,应用程序=明智的事物可能看起来不同。在这里,tinyint(1)
可以采取特殊的含义。例如,Connector/J(Java连接器)将tinyint(1)
视为布尔值,而不是将数字结果返回给应用程序,而是将值转换为true
和false
。这可以通过连接参数tinyInt1isBit=false
进行更改。
由于数据类型是8位(1字节),tinyint(1)可以保存范围在-128到127之间的数字 - 显然无符号tinyint可以保存值0-255。
它会悄悄地截断了值范围:
mysql> create table a
-> (
-> ttt tinyint(1)
->);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into a values (127);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values (-128);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values (128);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into a values (-129);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from a;
+------+
| ttt |
+------+
| 127 |
| -128 |
| 127 |
| -128 |
+------+
4 rows in set (0.00 sec)
mysql>
...除非你改变sql_mode
或更改服务器的配置:
mysql> set sql_mode=STRICT_ALL_TABLES;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into a values (-129);
ERROR 1264 (22003): Out of range value for column 'ttt' at row 1
mysql>
在DDL用于数据类型的值(例如:tinyint(1)),正如你怀疑的那样,是显示宽度。但是,它是可选的,客户端不必使用它。例如,标准的MySQL客户端不使用它。
https://dev.mysql.com/doc/refman/5.1/en/integer-types.html
https://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
MySql: Tinyint (2) vs tinyint(1) - what is the difference?
的可能重复的[MySQL的:?TINYINT(2)对TINYINT(1) - 哪些差(http://stackoverflow.com/questions/12839927/mysql-tinyint-2-vs-tinyint1-which -区别) – jmail