Home  >  Article  >  Database  >  mysql Int数据类型长度学习笔记

mysql Int数据类型长度学习笔记

WBOY
WBOYOriginal
2016-06-07 17:52:231082browse

mysql Int数据类型长度学习笔记 有需要了解的朋友可参考一下本文章。

1、数值类型    列类型 需要的存储量    TINYINT 1 字节    SMALLINT 2 个字节    MEDIUMINT 3 个字节    INT 4 个字节
int(M) 在 integer 数据类型中,M 表示最大显示宽度。
在 int(M) 中,M 的值跟 int(M) 所占多少存储空间并无任何关系。
和数字位数也无关系 int(3)、int(4)、int(8)
在磁盘上都是占用 4 btyes 的存储空间。

除了字段类型设 zerofill(补零)有点不同外,int(M) 跟 int 数据类型是相同的。
补零的情况:
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)
感觉就是为了排版方便


FLOAT、DOUBLE和DECIMAL的长度指的是全部数位(包括小数点后面的),例如DECIMAL(4,1)指的是全部位数为4,小数点后1位,如果插入1234,则查询的数据是999.9。过程如下

 
1.mysql> alter table test add realnum decimal(4,1);  
2.Query OK, 2 rows affected (0.03 sec)  
3.Records: 2  Duplicates: 0  Warnings: 0  
4. 
5.mysql> insert into test(id,realnum) values(2,1234);  
6.Query OK, 1 row affected, 1 warning (0.05 sec)  
7. 
8.mysql> select * from test;  
9.+------+---------+  
10.| id   | realnum |  
11.+------+---------+  
12.|  001 |    NULL |  
13.| 1234 |    NULL |  
14.|  002 |   999.9 |  
15.+------+---------+  
16.3 rows in set (0.02 sec) 

表列出了各种数值类型以及它们的允许范围和占用的内存空间。
    类型 大小 范围(有符号) 范围(无符号) 用途
    TINYINT 1 字节 (-128,127) (0,255) 小整数值
    SMALLINT 2 字节 (-32 768,32 767) (0,65 535) 大整数值
    MEDIUMINT 3 字节 (-8 388 608,8 388 607) (0,16 777 215) 大整数值
    INT或INTEGER 4 字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
    BIGINT 8 字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值
    FLOAT 4 字节 (-3.402 823 466 E+38,1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度
    浮点数值
    DOUBLE 8 字节 (1.797 693 134 862 315 7 E+308,2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度
    浮点数值
    DECIMAL 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 依赖于M和D的值 依赖于M和D的值 小数值
    INT 类型

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn