V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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 · Aug 9, 2017 · 4012 views
    This topic created in 3189 days ago, the information mentioned may be changed or developed.

    如下

    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 replies    2017-08-09 20:57:13 +08:00
    misaka19000
        1
    misaka19000  
       Aug 9, 2017 via Android
    为什么我在外面看标题是 zerofill,到里面就变成 unsigned 了?
    est
        2
    est  
       Aug 9, 2017
    因为不用显示 负号 了啊。。

    那个 10 11 表示显示宽度。

    4294967296 10 个字符
    -2147483648 11 个字符。
    MajorAdam
        3
    MajorAdam  
       Aug 9, 2017
    符号位
    LYEHIZRF
        4
    LYEHIZRF  
    OP
       Aug 9, 2017
    @est @MajorAdam soga 感谢 以前真没注意这个
    @misaka19000 哈哈因为马上 edit 了
    lanjz
        5
    lanjz  
       Aug 9, 2017
    MySQL 那个宽度对数值类型似乎不起作用? int(1) 和 int(10) 能存储的一样啊
    iyangyuan
        6
    iyangyuan  
       Aug 9, 2017 via iPhone
    如果不在数据库里做格式化,这个没什么用吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2531 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    ♥ Do have faith in what you're doing.