V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
LYEHIZRF
V2EX  ›  MySQL

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

  •  
  •   LYEHIZRF · 2017-08-09 11:43:32 +08:00 · 3502 次点击
    这是一个创建于 2445 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如下

    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)
    
    6 条回复    2017-08-09 20:57:13 +08:00
    misaka19000
        1
    misaka19000  
       2017-08-09 11:49:39 +08:00 via Android
    为什么我在外面看标题是 zerofill,到里面就变成 unsigned 了?
    est
        2
    est  
       2017-08-09 12:19:38 +08:00
    因为不用显示 负号 了啊。。

    那个 10 11 表示显示宽度。

    4294967296 10 个字符
    -2147483648 11 个字符。
    MajorAdam
        3
    MajorAdam  
       2017-08-09 13:09:47 +08:00
    符号位
    LYEHIZRF
        4
    LYEHIZRF  
    OP
       2017-08-09 13:39:16 +08:00
    @est @MajorAdam soga 感谢 以前真没注意这个
    @misaka19000 哈哈因为马上 edit 了
    lanjz
        5
    lanjz  
       2017-08-09 18:36:21 +08:00
    MySQL 那个宽度对数值类型似乎不起作用? int(1) 和 int(10) 能存储的一样啊
    iyangyuan
        6
    iyangyuan  
       2017-08-09 20:57:13 +08:00 via iPhone
    如果不在数据库里做格式化,这个没什么用吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1390 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:25 · PVG 01:25 · LAX 10:25 · JFK 13:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.