MySQL中int(m)的含义

2017-12-18 @后厂 

 

int(M): M indicates the maximum display width for integer types.

原来,在 int(M) 中,M 的值跟 int(M) 所占多少存储空间并无任何关系。

int(3)、int(4)、int(8) 在磁盘上都是占用 4 btyes 的存储空间。

说白了,除了显示给用户的方式有点不同外,int(M) 跟 int 数据类型是相同的。

mysql> drop table if exists t;
mysql> create table t(id int zerofill);
mysql> insert into t(id) values(10);
mysql> select * from t;
+------------+
| id         |
+------------+
| 0000000010 |
+------------+
mysql> alter table t change column id id int(3) zerofill;
mysql> select * from t;
+------+
| id   |
+------+
|  010 |
+------+
mysql>
mysql> alter table t change column id id int(4) zerofill;
mysql> select * from t;
+------+
| id   |
+------+
| 0010 |
+------+
mysql>
mysql> insert into t(id) values(1000000);
mysql> select * from t;
+---------+
| id      |
+---------+
|    0010 |
| 1000000 |
+---------+
  • 从上面的测试可以看出:
    • “(M)”指定了 int 型数值显示的宽度
    • 如果字段数据类型是 int(4),则:当显示数值 10 时,在左边要补上 “00”;
    • 当显示数值 100 是,在左边要补上“0”;
    • 当显示数值 1000000 时,已经超过了指定宽度“(4)”,因此按原样输出。
  • 补充字符串类型
char(n)     固定长度,最多255个字符
varchar(n)  可变长度,最多65535个字符
tinytext    可变长度,最多255个字符
text        可变长度,最多65535个字符
mediumtext  可变长度,最多2的24次方-1个字符
longtext    可变长度,最多2的32次方-1个字符
  • 补充日期类型
date        3字节,日期,格式:2014-09-18
time        3字节,时间,格式:08:42:30
datetime    8字节,日期时间,格式:2014-09-18 08:42:30
timestamp   4字节,自动存储记录修改的时间
year        1字节,年份

   

posted @ 2018-01-22 16:45  lixin[at]hitwh  阅读(546)  评论(0编辑  收藏  举报