Home >Database >Mysql Tutorial >mysql中int、bigint、smallint和tinyint的区别与长度_MySQL

mysql中int、bigint、smallint和tinyint的区别与长度_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:37:431243browse

bitsCN.com

mysql中int、bigint、smallint和tinyint的区别与长度

 

通过创建一张表,来看看 mysql 中 int bigint smallint 和 tinyint的区别与长度

1、在mysql 命令行创建如下表

 

01

CREATE TABLE `test_int_1` (

02

  `int_id` int NOT NULL,

03

  `bigint_id` bigint DEFAULT NULL,

04

  `bigint_25` bigint(25) DEFAULT NULL,

05

  `bigint_18` bigint(18) DEFAULT NULL,

06

  `int_8` int(8) DEFAULT NULL,

07

  `int_3` int(3) DEFAULT NULL,

08

  `smallint_id` smallint DEFAULT NULL,

09

  `tinyint_id` tinyint DEFAULT NULL,

10

  PRIMARY KEY (`int_id`)

11

) ENGINE=InnoDB DEFAULT CHARSET=utf8

2、desc

 

 

01

mysql> desc test_int_1;

02

+-------------+-------------+------+-----+---------+-------+

03

| Field       | Type        | Null | Key | Default | Extra |

04

+-------------+-------------+------+-----+---------+-------+

05

| int_id      | int(11)     | NO   | PRI | NULL    |       |

06

| bigint_id   | bigint(20)  | YES  |     | NULL    |       |

07

| bigint_25   | bigint(25)  | YES  |     | NULL    |       |

08

| bigint_18   | bigint(18)  | YES  |     | NULL    |       |

09

| int_8       | int(8)      | YES  |     | NULL    |       |

10

| int_3       | int(3)      | YES  |     | NULL    |       |

11

| smallint_id | smallint(6) | YES  |     | NULL    |       |

12

| tinyint_id  | tinyint(4)  | YES  |     | NULL    |       |

13

+-------------+-------------+------+-----+---------+-------+

14

8 rows in set (0.00 sec)

对比发现 int bigint smallint 和 tinyint 类型,如果创建新表时没有指定 int(M) 中的M时,默认分别是 :

 

int             -------     int(11)

 

bigint       -------     bigint(20)

 

smallint   -------     smallint(6)

 

tinyint     -------     tinyint(4)

 

下面是这几种类型的取值范围

 

mysql中int、bigint、smallint和tinyint的区别与长度_MySQL

 

MySQL还支持选择在该类型关键字后面的括号内指定整数值的显示宽度(例如,INT(4))。int(M) 在 integer 数据类型中,M 表示最大显示宽度,该可选显示宽度规定用于显示宽度小于指定的列宽度的值时从左侧填满宽度。

 

显示宽度并不限制可以在列内保存的值的范围,也不限制超过列的指定宽度的值的显示。

 

在 int(M) 中,M 的值跟 int(M) 所占多少存储空间并无任何关系。和数字位数也无关系, int(3)、int(4)、

 

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

 

当结合可选扩展属性ZEROFILL使用时, 默认补充的空格用零代替。例如,对于声明为INT(5) ZEROFILL的列,

 

值4检索为00004。

 

 bigint 用于某些特殊的情况,当整数值超过 int 数据类型支持的范围时,就可以采用 bigint。
 

bitsCN.com
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