create table `account`(
`id` int(3)
)engine = innodb default charset=utf8;
insert into `account`(`id`) values(12);
select * from `account`;
图中的数据刚好展示了3位,和我们的int(3)中的3符合。
alter table `account` change `id` `id` int(5) zerofill;
图中数据展示了5位,和int(n)中n的值的位数是一样的
insert into `account`(`id`) values(123456);
select * from `account`;
答案是:依然会展示插入的数据,不会丢失数据
我们知道MySQL中的int是占4个字节,每个字节8位,一共就是32位。
而且上面勾选了【Unsigned】,也就是无符号数,那么int的取值最大就可以取到
2^32 - 1 = 4294967295,我们把这个值插进去看看结果
insert into `account`(`id`) values(4294967295);
insert into `account`(`id`) values(4294967296);
通过以上几点,我们可以推断:
2^32 - 1 = 4294967295
,这个数值刚好10位数,超过这个数值的数无法插入数据库表中,自然也就无法展示,所以选择超过10的n值就没有太大意义。所以我们选择11位的展示长度肯定是够用了,(实际上写10就够用的),除此之外可能就是习惯了,但是具体的n取多少还是根据具体情况而定。
原文:https://www.cnblogs.com/xzhm/p/13816646.html