MySql: Tinyint (2) vs tinyint(1) - 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12839927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySql: Tinyint (2) vs tinyint(1) - what is the difference?
提问by realtebo
I knew boolean in mysql as tinyint (1).
我知道 mysql 中的 boolean 为tinyint (1).
Today I see a table with defined an integer like tinyint(2), and also others like int(4), int(6)...
今天我看到一个表定义了一个整数,比如tinyint(2),还有其他的,比如int(4), int(6)...
What does the size means in field of type integer and tinyint ?
大小在 integer 和 tinyint 类型的字段中是什么意思?
采纳答案by AamirR
It means display width
表示显示宽度
Whether you use tinyint(1) or tinyint(2), it does not make any difference.
无论您使用 tinyint(1) 还是 tinyint(2),都没有任何区别。
I always use tinyint(1) and int(11), I used several mysql clients (navicat, sequel pro).
我总是使用 tinyint(1) 和 int(11),我使用了几个 mysql 客户端(navicat、sequel pro)。
It does not mean anything AT ALL! I ran a test, all above clients or even the command-line client seems to ignore this.
它根本没有任何意义!我运行了一个测试,所有上述客户端甚至命令行客户端似乎都忽略了这一点。
But, display widthis most important if you are using ZEROFILLoption, for example your table has following 2 columns:
但是,如果您使用选项,显示宽度是最重要的ZEROFILL,例如您的表格有以下 2 列:
Atinyint(2) zerofill
甲TINYINT(2)ZEROFILL
Btinyint(4) zerofill
Btinyint(4) zerofill
both columns has the value of 1, output for column Awould be 01and 0001for B, as seen in screenshot below :)
两列具有1的值,对于列输出阿将是01和0001用于乙,如在下面的截图:)
回答by Ja?ck
The (m)indicates the column display width; applications such as the MySQL client make use of this when showing the query results.
的(m)指示列显示宽度; MySQL 客户端等应用程序在显示查询结果时会使用此功能。
For example:
例如:
| v | a | b | c |
+-----+-----+-----+-----+
| 1 | 1 | 1 | 1 |
| 10 | 10 | 10 | 10 |
| 100 | 100 | 100 | 100 |
Here a, band care using TINYINT(1), TINYINT(2)and TINYINT(3)respectively. As you can see, it pads the values on the left side using the display width.
这里a、b和c分别使用TINYINT(1)、TINYINT(2)和TINYINT(3)。如您所见,它使用显示宽度填充左侧的值。
It's important to note that it does not affect the accepted range of values for that particular type, i.e. TINYINT(1)still accepts [-128 .. 127].
重要的是要注意,它不会影响该特定类型的可接受值范围,即TINYINT(1)仍然接受[-128 .. 127]。
回答by zloctb
mysql> CREATE TABLE tin3(id int PRIMARY KEY,val TINYINT(10) ZEROFILL);
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO tin3 VALUES(1,12),(2,7),(4,101);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM tin3;
+----+------------+
| id | val |
+----+------------+
| 1 | 0000000012 |
| 2 | 0000000007 |
| 4 | 0000000101 |
+----+------------+
3 rows in set (0.00 sec)
mysql>
mysql> SELECT LENGTH(val) FROM tin3 WHERE id=2;
+-------------+
| LENGTH(val) |
+-------------+
| 10 |
+-------------+
1 row in set (0.01 sec)
mysql> SELECT val+1 FROM tin3 WHERE id=2;
+-------+
| val+1 |
+-------+
| 8 |
+-------+
1 row in set (0.00 sec)
回答by Devart
About the INT, TINYINT... These are different data types, INT is 4-byte number, TINYINT is 1-byte number. More information here - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT.
关于INT,TINYINT...这些是不同的数据类型,INT是4字节数,TINYINT是1字节数。更多信息在这里 - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT。
The syntax of TINYINT data type is TINYINT(M), where M indicates the maximum display width (used only if your MySQL client supports it).
TINYINT 数据类型的语法为 TINYINT(M),其中 M 表示最大显示宽度(仅在您的 MySQL 客户端支持时使用)。


