括号中使用的数字1仅用于宽度显示。 INT(1)和TINYINT(1)不影响存储。
TINYINT占用1个字节,这意味着它的范围是-128到+127,而int占用4个字节;它的范围是 -2147483648 到 +2147483647
要了解宽度显示,让我们创建一个表格 -
mysql> create table intAndTinyint
−> (
−> FirstNumber int(1) zerofill,
−> SecondNumber tinyint(1) zerofill
−> );
Query OK, 0 rows affected (0.52 sec)
现在您可以在表中插入记录。查询如下 -
mysql> insert into intAndTinyint values(1,1);
Query OK, 1 row affected (0.32 sec)
mysql> insert into intAndTinyint values(12,12);
Query OK, 1 row affected (0.26 sec)
mysql> insert into intAndTinyint values(123,123);
Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录。查询如下 -
mysql> select *from intAndTinyint;
以下是输出 -
+-------------+--------------+
| FirstNumber | SecondNumber |
+-------------+--------------+
| 1 | 1 |
| 12 | 12 |
| 123 | 123 |
+-------------+--------------+
3 rows in set (0.00 sec)
当括号的数字 1 通过填零增加到大于 1 时,您就会明白这一点。让我们看一个仅用于 INT 的示例来理解宽度填零的概念。
创建一个表。以下是创建表的查询 -
mysql> create table intVsIntAnyThingDemo
−> (
−> Number1 int(11) unsigned zerofill,
−> Number int(13) unsigned zerofill
−> );
Query OK, 0 rows affected (1.17 sec)
现在您可以借助插入命令在表中插入记录。这里,我们为INT设置了不同的宽度。查询如下 -
mysql> insert into intVsIntAnyThingDemo values(12345,6789);
Query OK, 1 row affected (0.44 sec)
mysql> insert into intVsIntAnyThingDemo values(3,2);
Query OK, 1 row affected (0.20 sec)
mysql> insert into intVsIntAnyThingDemo values(12,89);
Query OK, 1 row affected (0.15 sec)
mysql> insert into intVsIntAnyThingDemo values(123,6789);
Query OK, 1 row affected (0.17 sec)
mysql> insert into intVsIntAnyThingDemo values(1234,6789);
Query OK, 1 row affected (0.14 sec)
借助select语句显示所有记录。查询如下 -
mysql> select *from intVsIntAnyThingDemo;
以下是显示不同宽度和零填充的输出
+-------------+---------------+
| Number1 | Number |
+-------------+---------------+
| 00000012345 | 0000000006789 |
| 00000000003 | 0000000000002 |
| 00000000012 | 0000000000089 |
| 00000000123 | 0000000006789 |
| 00000001234 | 0000000006789 |
+-------------+---------------+
5 rows in set (0.00 sec)