当前位置:Gxlcms > 数据库问题 > Mysql数据类型

Mysql数据类型

时间:2021-07-01 10:21:17 帮助过:18人阅读

the maximum display width for integer types.翻译成中文的意思就是,这里括号里面的M代表了最大的显示长度,而不是存储的长度。

1、整型

MySQL数据类型   含义(有符号)
tinyint(m)  1个字节  范围(-128~127)
smallint(m) 2个字节  范围(-32768~32767)
mediumint(m)    3个字节  范围(-8388608~8388607)
int(m)  4个字节  范围(-2147483648~2147483647)
bigint(m)   8个字节  范围(+-9.22*10的18次方)

  根据上面的信息可以得出结论:不管是int(10)还是int(9),最终都占四个字节,既然占的字节数是一个定值,那么最终能保存的数据范围也是一个定值。所以“int(10)就能保存一个10位的数字,int(9)就只能保存一个长度是9位的数字。是不对的。

  原来,在 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)”,我想不出有何意义。 

  但是,char(n)、varchar(n)又和int(n)里面的n是不一样的含义。我们再看一下官方的解释。

  

第一:讨论char(n)

The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length.When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH [581]SQL mode is enabled.

手册中指出,长度的限制是0-255,经过验证,长度的限制是0-255个字符。也就是说,没有明确标出字节的限制数。

也就是说,char(2)最多可以存储两个字符长度的字符串,char(5)最多可以存储五个字符长度的字符串。char(n)中的n限定了所能存储的字符个数。

第二:讨论varchar(m)

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535.The effective maximum length of a VARCHARis subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. See Section E.10.4, “Table Column-Count and Row-Size Limits”.

 官方文档指出,varchar(m)中的m限定了varchar所能存储的字节个数

下面补充一下数据类型 
1、整型

1 MySQL数据类型   含义(有符号)
2 tinyint(m)  1个字节  范围(-128~127)
3 smallint(m) 2个字节  范围(-32768~32767)
4 mediumint(m)    3个字节  范围(-8388608~8388607)
5 int(m)  4个字节  范围(-2147483648~2147483647)
6 bigint(m)   8个字节  范围(+-9.22*10的18次方)

取值范围如果加了unsigned,则最大值翻倍,如tinyint unsigned的取值范围为(0~256)。 
int(m)里的m是表示SELECT查询结果集中的显示宽度,并不影响实际的取值范围,没有影响到显示的宽度,不知道这个m有什么用。

2、浮点型(float和double)

1 MySQL数据类型   含义
2 float(m,d)  单精度浮点型    8位精度(4字节)     m总个数,d小数位
3 double(m,d) 双精度浮点型    16位精度(8字节)    m总个数,d小数位

设一个字段定义为float(5,3),如果插入一个数123.45678,实际数据库里存的是123.457,但总个数还以实际为准,即6位。 
3、定点数 
浮点型在数据库中存放的是近似值,而定点类型在数据库中存放的是精确值。

1 decimal(m,d) 参数m<65 是总个数,d<30且 d<m 是小数位。

4、字符串(char,varchar,_text)

1 MySQL数据类型   含义
2 char(n)     固定长度,最多255个字符
3 varchar(n)  可变长度,最多65535个字符
4 tinytext    可变长度,最多255个字符
5 text        可变长度,最多65535个字符
6 mediumtext  可变长度,最多2的24次方-1个字符
7 longtext    可变长度,最多2的32次方-1个字符

 

5、日期和时间数据类型

1 MySQL数据类型   含义
2 date        3字节,日期,格式:2014-09-18
3 time        3字节,时间,格式:08:42:30
4 datetime    8字节,日期时间,格式:2014-09-18 08:42:30
5 timestamp   4字节,自动存储记录修改的时间
6 year        1字节,年份

 

Mysql数据类型

标签:官方文档   补充   idt   val   数据   limits   ext   sig   variable   

人气教程排行