mysql int 整数类型 解释显示宽度 和 存储宽度

 

存储宽度 是实际存储记录宽度

存储宽度默认是写死的,就算修改宽度也改变不了,改变的是显示宽度

============有符号和无符号int=============

创建一个 无符号的 int 整数类型 

mysql> create table t3(id int(1) unsigned);
Query OK, 0 rows affected (0.01 sec)

mysql> desc t3;
+-------+-----------------+------+-----+---------+-------+
| Field | Type            | Null | Key | Default | Extra |
+-------+-----------------+------+-----+---------+-------+
| id    | int(1) unsigned | YES  |     | NULL    |       |
+-------+-----------------+------+-----+---------+-------+
1 row in set (0.00 sec)


mysql> insert into t3 values(25555555);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t3;
+----------+
| id       |
+----------+
| 25555555 |
+----------+
1 row in set (0.00 sec)


整数类型 最大  4个字节存储

无符号int类型 最大 这个数 4294967295   无符号范围0-4294967295 

mysql> insert into t3 values(25555555555555555555555555555555555);
Query OK, 1 row affected, 2 warnings (0.00 sec)

mysql> select * from t3;
+------------+
| id         |
+------------+
|   25555555 |
| 4294967295 |
+------------+
2 rows in set (0.00 sec)


自己定义宽度的 int(1) 没有限制现在存储 ,因为 mysql  不管是 tinyint 、int、bigint 整数类型 都不用设置宽度的
整数类型的那个设置宽度 不是存储宽度,而是显示宽度


对于tinyint、int、bigint的存储宽度 mysql已经固定死了,例如用tinyint mysql只用1个字节
int用4个字节、bigint8个字节,能改变的只有显示宽度



显示宽度设置5,int 最大存储宽度是4个字节
mysql> create table t4(id int(5) unsigned);
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------+
| Tables_in_db4 |
+---------------+
| t1            |
| t2            |
| t3            |
| t4            |
+---------------+
4 rows in set (0.00 sec)


mysql> desc t4;
+-------+-----------------+------+-----+---------+-------+
| Field | Type            | Null | Key | Default | Extra |
+-------+-----------------+------+-----+---------+-------+
| id    | int(5) unsigned | YES  |     | NULL    |       |
+-------+-----------------+------+-----+---------+-------+
1 row in set (0.00 sec)


mysql> insert into t4 values(1),(255);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t4;
+------+
| id   |
+------+
|    1 |
|  255 |
+------+
2 rows in set (0.00 sec)
显示宽度 是查询表的时候,显示的结果的宽度

======用zerofill测试整数类型的显示宽度=============

再创建一张表t5 加上zerofill用0填充,加上这个可以看到显示宽度意思
mysql> create table t5(id int(5) zerofill);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t5 values(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> 
mysql> select * from t5;
+-------+
| id    |
+-------+
| 00001 |
| 00002 |
+-------+
2 rows in set (0.00 sec)
如果插入的记录的宽度 超过设置的显示宽度他会正常显示

mysql> insert into t5 values(111111111111111111111111111111111111111111);
Query OK, 1 row affected, 2 warnings (0.00 sec)


mysql> select * from t5;
+------------+
| id         |
+------------+
|      00001 |
|      00002 |
| 4294967295 |
+------------+
3 rows in set (0.00 sec)

这个是显示宽度

不设置宽度时候
mysql> create table t6(id int unsigned);
Query OK, 0 rows affected (0.01 sec)

mysql> desc t6;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | YES  |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
无符号int整数类型默认宽度是10字节,有符号int整数类型默认宽度是11字节


默认不加任何东西就是 有符号int整数类型
mysql> create table t7(id int);
Query OK, 0 rows affected (0.02 sec)

mysql> desc t7;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)


注意:为该类型指定宽度时,仅仅只是指定查询结果的显示宽度,与存储范围无关,存储范围如下

其实我们完全没必要为整数类型指定宽度,使用默认的就可以了

 

默认的显示宽度,都是在最大值的基础上加1

对于整数类型 没有必要设置宽度 设置的宽度是显示宽度,对于其他类型来说设置的宽度是  存储宽度



int的存储宽度是4个Bytes,即32个bit,即2**32

无符号最大值为:4294967296-1


有符号最大值:2147483648-1


有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,所以int类型默认的显示宽度为11是非常合理的


最后:整形类型,其实没有必要指定显示宽度,使用默认的就ok

原文地址:https://www.cnblogs.com/mingerlcm/p/9756145.html