更改 int 字段为 unsigned 后默认宽度就从 11 变成 10 是什么原因

2017-08-09 11:43:32 +08:00
 LYEHIZRF

如下

mysql> desc a1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.23 sec)

mysql> alter table a1 modify id int zerofill;
Query OK, 0 rows affected (0.55 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc a1;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type                      | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id    | int(10) unsigned zerofill | YES  |     | NULL    |       |
| name  | varchar(20)               | YES  |     | NULL    |       |
+-------+---------------------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql> alter table a1 modify id int(11) zerofill;
Query OK, 0 rows affected (0.36 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc a1;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type                      | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id    | int(11) unsigned zerofill | YES  |     | NULL    |       |
| name  | varchar(20)               | YES  |     | NULL    |       |
+-------+---------------------------+------+-----+---------+-------+
2 rows in set (0.03 sec)
    
mysql> alter table a1 modify id int unsigned;
Query OK, 0 rows affected (0.31 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc a1;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | YES  |     | NULL    |       |
| name  | varchar(20)      | YES  |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.06 sec)
3508 次点击
所在节点    MySQL
6 条回复
misaka19000
2017-08-09 11:49:39 +08:00
为什么我在外面看标题是 zerofill,到里面就变成 unsigned 了?
est
2017-08-09 12:19:38 +08:00
因为不用显示 负号 了啊。。

那个 10 11 表示显示宽度。

4294967296 10 个字符
-2147483648 11 个字符。
MajorAdam
2017-08-09 13:09:47 +08:00
符号位
LYEHIZRF
2017-08-09 13:39:16 +08:00
@est @MajorAdam soga 感谢 以前真没注意这个
@misaka19000 哈哈因为马上 edit 了
lanjz
2017-08-09 18:36:21 +08:00
MySQL 那个宽度对数值类型似乎不起作用? int(1) 和 int(10) 能存储的一样啊
iyangyuan
2017-08-09 20:57:13 +08:00
如果不在数据库里做格式化,这个没什么用吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/381603

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX