我不明白为什么会有这个选项。 带符号的 TINYINT 数据类型可以存储 -128 到 127 之间的整数值。
mysql> create table b (i tinyint(1));
mysql> insert into b values (42);
mysql> select * from b;
+------+
| i |
+------+
| 42 |
+------+
最佳答案
数据方面,tinyint(1)
、tinyint(2)
、tinyint(3)
等都是完全一样的。对于 SIGNED
,它们都在 -128 到 127 范围内,对于 UNSIGNED
,它们都在 0-255 范围内。正如其他答案所指出的那样,括号中的数字只是一个显示宽度提示。
不过,您可能需要注意,application=wise 看起来可能有所不同。在这里,tinyint(1)
可以有特殊的含义。例如,Connector/J(Java 连接器)将 tinyint(1)
视为 bool 值,而不是将数值结果返回给应用程序,而是将值转换为 true
和 false
。这可以通过 tinyInt1isBit=false
连接参数更改。
由于数据类型为 8 位(1 字节),tinyint(1) 可以容纳 -128 到 127 范围内的数字 - 显然,unsigned 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(parameter)是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112203/