mysql中的int(M)

在使用pdm设计表结构时,经常把int类型设置成int(11)。

于是问自己,int类型存储时不就是占4个字节的存储空间吗?怎么mysql字段里的int还能设计成不同长度的?
测试了一把,发现其实这个长度跟存储没什么关系,存储时都是占4个字节的。
CREATE TABLE `test1` (
  `IDX` bigint(20) NOT NULL AUTO_INCREMENT,
  `ID` int(1) DEFAULT NULL,
  `ID2` int(3) DEFAULT NULL,
  `ID3` int(11) DEFAULT NULL,
  `ID4` int(11) DEFAULT NULL,
  `ID5` int(11) DEFAULT NULL,
  PRIMARY KEY (`IDX`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
 
mysql中的int(M)

int(M)里的M表示最大显示宽度,当加上zerofill才会表现出效果来。比如 int(3) zerofill,你插入到数据库里的是10,则实际插入为010,也就是在前面补充加了一个0.如果int(3)和int(10)不加 zerofill,则它们没有什么区别.M不是用来限制int个数的.int(M)的最大值和最小值与undesigned有关。借用网上的结论:
mysql> desc test;
+-------+--------------------------+------+-----+---------+----------------+
| Field | Type                     | Null | Key | Default | Extra          |
+-------+--------------------------+------+-----+---------+----------------+
| id    | int(3) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
+-------+--------------------------+------+-----+---------+----------------+
1 row in set (0.01 sec)
 
mysql> select * from test ;
 
+------+
 
| id   |
 
+------+
 
|  001 |
 
|  010 |
 
| 1234 |
 
+------+
 
3 rows in set (0.00 sec)
 
不补零:
 
mysql> desc test;
+-------+--------+------+-----+---------+----------------+
| Field | Type   | Null | Key | Default | Extra          |
+-------+--------+------+-----+---------+----------------+
| id    | int(3) | NO   | PRI | NULL    | auto_increment |
+-------+--------+------+-----+---------+----------------+
1 row in set (0.01 sec)
 
mysql> select * from test ;
+------+
| id   |
+------+
|    1 |
|   10 |
| 1234 |
+------+
3 rows in set (0.00 sec)
 
轩自:http://blog.sina.com.cn/s/blog_52ef93720101a45f.html
 
附:
posted @ 2015-05-25 10:28  stma  阅读(1477)  评论(0)    收藏  举报