参数TINYINT(parameter)的含义是什么? [英] What is the meaning parameter TINYINT(parameter)?
问题描述
我不明白为什么选择这个选项. 带符号的TINYINT数据类型可以存储-128到127之间的整数值.
I can not understand why this option. The signed TINYINT data type can store integer values between -128 and 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之间.正如其他答案所指出的那样,括号中的数字仅是显示宽度的提示.
Data-wise, tinyint(1)
, tinyint(2)
, tinyint(3)
etc. are all exactly the same. They are all in the range -128 to 127 for SIGNED
or 0-255 for UNSIGNED
. As other answers noted the number in parenthesis is merely a display width hint.
不过,您可能要注意,应用程序=明智的事物看起来可能有所不同.在这里,tinyint(1)
可能具有特殊含义.例如,Connector/J(Java连接器)将tinyint(1)
视为布尔值,并且不将数值结果返回给应用程序,而是将值转换为true
和false
.可以通过tinyInt1isBit=false
连接参数进行更改.
You might want to note, though, that application=wise things may look different. Here, tinyint(1)
can take a special meaning. For example, the Connector/J (Java connector) treats tinyint(1)
as a boolean value, and instead of returning a numerical result to the application, it converts values to true
and false
. this can be changed via the tinyInt1isBit=false
connection parameter.
由于数据类型为8位(1字节),tinyint(1)可以容纳-128到127之间的数字-显然,无符号的tinyint可以容纳值0-255.
A tinyint(1) can hold numbers in the range -128 to 127, due to the datatype being 8 bits (1 byte) - obviously an unsigned tinyint can hold values 0-255.
它会自动截断超出范围的值:
It will silently truncate out of range values:
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
或更改服务器配置:
... unless you change the sql_mode
or change the server config:
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客户端不使用它.
The value used in the DDL for the datatype (eg: tinyint(1)) is, as you suspected, the display width. However, it is optional and clients don't have to use it. The standard MySQL client doesn't use it, for example.
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)-有什么区别?
这篇关于参数TINYINT(parameter)的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!