MySQL数据类型 int(M) 表示什么意思?
2010-10-15 10:03
465 查看
MySQL 数据类型中的 integer types 有点奇怪。你可能会见到诸如:int(3)、int(4)、int(8) 之类的 int 数据类型。刚接触 MySQL 的时候,我还以为 int(3) 占用的存储空间比 int(4) 要小, int(4) 占用的存储空间比 int(8) 小。
后来,参看 MySQL 手册,发现自己理解错了。
int(M): M indicates the maximum display width for integer types.
在 integer 数据类型中,M 表示最大显示宽度。
原来,在 int(M) 中,M 的值跟 int(M) 所占多少存储空间并无任何关系。 int(3)、int(4)、int(8) 在磁盘上都是占用 4 btyes 的存储空间。说白了,除了显示给用户的方式有点不同外,int(M) 跟 int 数据类型是相同的。
另外,int(M) 只有跟 zerofill 结合起来,才能使我们清楚的看到不同之处。
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)”,因此按原样输出。
在使用 MySQL 数据类型中的整数类型(tinyint, smallint, mediumint, int/integer, bigint)时,非特殊需求下,在数据类型后加个“(M)”,我想不出有何意义。
另外,在 MySQL 数据类型中,integer 和 int 同义。到底使用哪个,自己看着办吧。
后来,参看 MySQL 手册,发现自己理解错了。
int(M): M indicates the maximum display width for integer types.
在 integer 数据类型中,M 表示最大显示宽度。
原来,在 int(M) 中,M 的值跟 int(M) 所占多少存储空间并无任何关系。 int(3)、int(4)、int(8) 在磁盘上都是占用 4 btyes 的存储空间。说白了,除了显示给用户的方式有点不同外,int(M) 跟 int 数据类型是相同的。
另外,int(M) 只有跟 zerofill 结合起来,才能使我们清楚的看到不同之处。
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)”,因此按原样输出。
在使用 MySQL 数据类型中的整数类型(tinyint, smallint, mediumint, int/integer, bigint)时,非特殊需求下,在数据类型后加个“(M)”,我想不出有何意义。
另外,在 MySQL 数据类型中,integer 和 int 同义。到底使用哪个,自己看着办吧。
相关文章推荐
- MySQL数据类型 int(M) 表示什么意思?
- MySQL数据类型 int(M) 表示什么意思?详解mysql int类型的长度值问题
- MySQL 中你应该使用什么数据类型表示时间?
- MySQL 中你应该使用什么数据类型表示时间?
- MySQL 中你应该使用什么数据类型表示时间?
- MySQL 中使用什么数据类型表示时间
- MySQL 中你应该使用什么数据类型表示时间?
- MySQL 中你应该使用什么数据类型表示时间?
- MySQL 各种类型int表示范围和size
- JAVA中有关byte,int,long这些基本数据类型的存储方式以及负数在JAVA中的表示方式
- mysql从csv文件导入数据时提示int类型出现' '(空字符串)
- MYSQL存储经纬度使用什么数据类型
- C/C++语言中的int等基本数据类型所能表示的最大值最小值
- c语言中计算int,float,double,char四种数据类型所能表示的数据范围
- 关于C和C++中的基本数据类型int、long、long long、float、double、char、string的大小及表示范围
- MySql 基础学习笔记 1——概述与基本数据类型: 整型: 1)TINYINT 2)SMALLINT 3) MEDIUMINT 4)INT 5)BIGINT 主要是大小的区别 图 浮点型:命令
- tensorflow 张量的阶、形状、数据类型及None在tensor中表示的意思。
- 关于MySQL数据类型定义的几个细节-INT(N)/VARCHAR(N)/DECIMAL(M,N)
- mysql的数据类型int、bigint、smallint 和 tinyint取值范围 及varchar
- mysql数据类型---数值型---int