面试题 int(3) int(10) 区别

1.MySQL 中 int(3) int(10) 区别

答案

存储大小并无差异,只是不足位数的时候,左边补0.

补充知识点

创建数据库表时,加zerofill ,可以看出效果。mysql 默认 int(11) unsign int(10)

int类型,占4个字节,一个字节8位,4*8 = 32 位长,所以int可以表示2的32次方个数。无符号整型0~4 294 967 295,或有符号整型 -2147483648~2147483647.

有符号数最小值:
 -   2  1  4  7  4  8  3  6  4   8
 1  2  3  4  5  6  7  8  9  10 11    --共11位

 无符号数最大值:
 4  2  9  4  9  6  7  2  9  5
 1  2  3  4  5  6  7  8  9  10        --共10位

实例演示

create table tb_int(
int_1 INT(10) UNSIGNED ZEROFILL,
int_2 INT(5) UNSIGNED ZEROFILL,
int_3 INT UNSIGNED ZEROFILL,
int_4 INT ZEROFILL,
int_5 INT(11) ZEROFILL,
int_6 INT(5)  ZEROFILL
)

insert into tb_int VALUES(123,123,123,123,123,123);

select* from tb_int

结果

参考文档

为什么int类型字段在mysql中默认int(11) 无符号默认int(10)?
Mysql中,int(10)和int(11)的区别
MySQL中int(M)和tinyint(M)数值类型中M值的意义

posted @ 2019-08-28 14:45  lick  阅读(759)  评论(0编辑  收藏  举报