MySQL int(M)了解

来源:互联网 发布:购物车数据库表设计 编辑:程序博客网 时间:2024/06/03 01:28

MySQL int(M)了解

在MySQL创建表时,通常会看到某个int字段,定义为 a int(10) b int(11)....
疑问,那么这里的10,11..,是什么?

查询了下,只有在加 zerofill时,才能看出效果,否则,select结果出来一样,但是,
我们姑且叫了"宽度"
create table t1(Id int(3) zerofill) engine=InnoDB;
insert into t1 select 1;

insert into t1 select 11;

insert into t1 select 111;

insert into t1 select 1111;

查询如下:
mysql> select * from t1;
+------+
| Id   |
+------+
|  001 |
|  011 |
|  111 |
| 1111 |
+------+
7 rows in set (0.00 sec)
create table t2(Id int(3))engine=InnoDB;
insert into t2 select 1;
insert into t2 select 11;
insert into t2 select 111;
查询结果如下:
mysql> select * from t2;
+------+
| Id   |
+------+
|    1 |
|   11 |
|  111 |
+------+
3 rows in set (0.00 sec)

综上所述:当你输入11时,会默认给你存储011,那么int(3)中3就是一数据长度(宽度),当你不足3位时,帮你补全,超过

3,也无影响;如int(1)和int(10)存储格式上有点区别,使用时没有不同,只在zerofill下才可看出点点效果.

注意:不论是int(1),int(10)...数据库存储或数据占的空间是4个字节长度,和这个1..10没有关系;有关系是在定义

int类型是有无unsigned(符号),存储数据范围不同;




0 0