MYSQL中,int(n)括号里面的数据n无论写成多少,都是占4个字节的空间,最多能够存10位数。N不代表能够存多少位数,显示宽度M与数据所占用空间,数值的范围无关。 如果在定义字段的时候指定zerofill,那么当数值的显示宽度小于指定的列宽度时候,则默认补充的空格用0代替,例如定义了zerofill,int(4)中的4就有意义了,我们插入一个数1,他会补充完成0001,int(10)则为0000000001。
测试如下:
1、创建一张int(4)的测试表
mysql> create table t1(id int(4));
Query OK, 0 rows affected (0.08 sec)
mysql> insert into t1 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values(2);
Query OK, 1 row affected (0.02 sec)
mysql> insert into t1 values(2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 2 |
+------+
3 rows in set (0.00 sec)
2、创建一张int(10)的测试表
mysql> create table t2(id int(10));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t2 values(1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t2 values(2);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t2 values(3);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
3、创建一张int(4) zerofill的测试表
mysql> create table t3(id int(4) zerofill);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t3 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t3 values(2);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t3 values(3);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t3;
+------+
| id |
+------+
| 0001 |
| 0002 |
| 0003 |
+------+
3 rows in set (0.00 sec)
自动补齐前面的0。
4、创建一张int(10) zerofill的测试表
mysql> create table t4(id int(10) zerofill);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t4 values(1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t4 values(2);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t4 values(3);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM t4;
+------------+
| id |
+------------+
| 0000000001 |
| 0000000002 |
| 0000000003 |
+------------+
3 rows in set (0.00 sec)
原文地址:https://www.cnblogs.com/hmwh/p/9070907.html