当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL Integer类型与INT(11)

MySQL Integer类型与INT(11)

2020年03月16日  | 移动技术网IT编程  | 我要评论

1.介绍

integer类型,即整数类型,mysql支持的整数类型有tinyint、smallint、mediumint、int、bigint。

 

1.1 空间和范围

每种整数类型所需的存储空间和范围如下:

类型 字节

最小值

(有符号)

最大值

(有符号)

最小值

(无符号)

最大值

(无符号)

tinyint 1 -128 127 0 255
smallint 2 -32768 32767 0 65535
mediumint 3 -8388608 8388607 0 16777215
int 4 -2147483648 2147483647 0 4294967295
bigint 8

-263

(-9223372036854775808)

263-1

(9223372036854775807)

0

264-1

(18446744073709551615)

 

2. int(11)

2.1 数字是否限制长度?

id int(11) not null auto_increment,

在一些建表语句会出现上面 int(11) 的类型,那么其代表什么意思呢?

对于integer类型括号中的数字称为字段的显示宽度。这与其他类型字段的含义不同。对于decimal类型,表示数字的总数。对于字符字段,这是可以存储的最大字符数,例如varchar(20)可以存储20个字符。

显示宽度并不影响可以存储在该列中的最大值。int(5) 和 int(11)可以存储相同的最大值。哪怕设置成 int(20) 并不意味着将能够存储20位数字(bigint),该列还是只能存储int的最大值。

示例

创建一个临时表:

create temporary table demo_a (
	id int(11) not null auto_increment,
	a int(1) not null,
	b int(5) not null,
	primary key (`id`)
)

插入超过"长度"的数字:

insert into demo_a(a,b) values(255, 88888888);

查看结果:发现数字并不是设置长度

mysql> select * from demo_a;
+----+-----+----------+
| id | a   | b        |
+----+-----+----------+
|  1 | 255 | 88888888 |
+----+-----+----------+
1 row in set (0.03 sec)

 

2.2 数字表达什么意思?

当列设置为unsigned zerofill时,int(11)才有意义,其表示的意思为如果要存储的数字少于11个字符,则这些数字将在左侧补零。

注意:zerofill默认的列为无符号,因此不能存储负数。

示例

创建一个临时表:b列设置为unsigned zerofill

create temporary table demo_a (
	id int(11) not null auto_increment,
	a int(11) not null,
	b int(11) unsigned zerofill not null,
	primary key (`id`)
);

 插入数值:

insert into demo_a(a,b) values(1, 1);

 结果:b列的左侧使用了0填充长度

mysql> select * from demo_a;
+----+---+-------------+
| id | a | b           |
+----+---+-------------+
|  1 | 1 | 00000000001 |
+----+---+-------------+
1 row in set (0.18 sec)

  

3. 参考资料

integer类型

what does int(11) means in mysql?

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网