代码之家  ›  专栏  ›  技术社区  ›  Parris

mysql中的类型:bigint(20)vs int(20)

  •  178
  • Parris  · 技术社区  · 16 年前

    我想知道两者之间有什么区别 BigInt , MediumInt Int 是。。。很明显,他们会允许更大的数字;但是,我可以 Int(20) 或A BigInt(20) 这就使得它看起来不一定是关于大小的。

    有些洞见会很好,只是有点好奇。我一直在用 MySQL 有一段时间,在选择类型时尝试应用业务需求,但我从未理解这方面。

    6 回复  |  直到 7 年前
        1
  •  381
  •   Bill Karwin    8 年前

    http://dev.mysql.com/doc/refman/8.0/en/numeric-types.html

    • INT 是一个四字节有符号整数。

    • BIGINT 是一个8字节有符号整数。

    They each accept no more and no fewer values than can be stored in their respective number of bytes. 这意味着2 三十二 AN中的值 国际的 和2 六十四 A中的值 大整数 .

    20里 INT(20) BIGINT(20) 几乎毫无意义。这是显示宽度的提示。它与存储无关,也与列将接受的值的范围无关。

    实际上,它只影响 ZEROFILL 选项:

    CREATE TABLE foo ( bar INT(20) ZEROFILL );
    INSERT INTO foo (bar) VALUES (1234);
    SELECT bar from foo;
    
    +----------------------+
    | bar                  |
    +----------------------+
    | 00000000000000001234 |
    +----------------------+
    

    这是MySQL用户常见的困惑源 INT(20) 假设这是一个尺寸限制,类似于 CHAR(20) . 事实并非如此。

        2
  •  38
  •   John Feminella    16 年前

    类型声明中括号中的数字是 显示宽度 与可以存储在数据类型中的值的范围无关。因为你可以申报 Int(20) 并不意味着您可以在其中存储最多10^20个值:

    [...] This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. …

    显示宽度不限制可以存储在列中的值的范围,也不限制为宽度超过为该列指定的值显示的位数。 例如,指定为smallint(3)的列通常具有-32768到32767的smallint范围,并且超出三个字符允许范围的值使用三个以上的字符显示。

    有关可以存储在每个mysql数据类型中的最大值和最小值的列表,请参见 here .

        3
  •  12
  •   OMG Ponies    16 年前

    Quote :

    “bigint(20)”规范不是数字限制。这仅仅意味着,当数据显示时,如果它使用的数字少于20位,它将用零填充。2^64是bigint类型的硬限制,本身有20个数字,因此bigint(20)只意味着小于10^20的所有内容都将在显示时用空格填充。

        4
  •  1
  •   Sergey Podgornyy    8 年前

    As far as I know, there is only one small difference is when you are trying to insert value which is out of range.

    在示例中,我将使用 401421228216 ,这就是 101110101110110100100011101100010111000 (长度 三十九 字符)

    • 如果你有 INT(20) 对于系统,这意味着在内存中分配至少20位。但是如果你要插入大于 2^20 ,只有当它小于 INT(32) -> 2147483647 (或) 2 * INT(32) -> 4294967295 对于 UNSIGNED )

    例子:

    mysql> describe `test`;
    +-------+------------------+------+-----+---------+-------+
    | Field | Type             | Null | Key | Default | Extra |
    +-------+------------------+------+-----+---------+-------+
    | id    | int(20) unsigned | YES  |     | NULL    |       |
    +-------+------------------+------+-----+---------+-------+
    1 row in set (0,00 sec)
    
    mysql> INSERT INTO `test` (`id`) VALUES (401421228216);
    ERROR 1264 (22003): Out of range value for column 'id' at row 1
    
    mysql> SET sql_mode = '';
    Query OK, 0 rows affected, 1 warning (0,00 sec)
    
    mysql> INSERT INTO `test` (`id`) VALUES (401421228216);
    Query OK, 1 row affected, 1 warning (0,06 sec)
    
    mysql> SELECT * FROM `test`;
    +------------+
    | id         |
    +------------+
    | 4294967295 |
    +------------+
    1 row in set (0,00 sec)
    
    • 如果你有 BIGINT(20) 对于系统,这意味着在内存中分配至少20位。但是如果你要插入大于 2 ^ 20 ,它将成功存储,如果小于 BIGINT(64) -> 9223372036854775807 (或) 2 * BIGINT(64) -> 18446744073709551615 对于 未签名的 )

    例子:

    mysql> describe `test`;
    +-------+---------------------+------+-----+---------+-------+
    | Field | Type                | Null | Key | Default | Extra |
    +-------+---------------------+------+-----+---------+-------+
    | id    | bigint(20) unsigned | YES  |     | NULL    |       |
    +-------+---------------------+------+-----+---------+-------+
    1 row in set (0,00 sec)
    
    mysql> INSERT INTO `test` (`id`) VALUES (401421228216);
    Query OK, 1 row affected (0,04 sec)
    
    mysql> SELECT * FROM `test`;
    +--------------+
    | id           |
    +--------------+
    | 401421228216 |
    +--------------+
    1 row in set (0,00 sec)
    
        5
  •  1
  •   Yousha Aleayoub Albert    8 年前

    I wanted to add one more point is, if you are storing a really large number like 902054990011312 then one can easily see the difference of INT(20) BIGINT(20) . 建议存放在 BIGINT .

        6
  •  0
  •   Jayhello    8 年前

    让我们举一个int(10)的例子,一个带有zerofill关键字,一个不带,表如下所示:

    create table tb_test_int_type(
        int_10 int(10),
        int_10_with_zf int(10) zerofill,
        unit int unsigned
    );
    

    让我们插入一些数据:

    insert into tb_test_int_type(int_10, int_10_with_zf, unit)
    values (123456, 123456,3147483647), (123456, 4294967291,3147483647) 
    ;
    

    然后

    select * from tb_test_int_type; 
    
    # int_10, int_10_with_zf, unit
    '123456', '0000123456', '3147483647'
    '123456', '4294967291', '3147483647'
    

    我们可以看到

    • 带关键字 zerofill ,小于10的num将填充0,但不填充 零填充 它不会

    • 其次是关键字 零填充 ,int_10_with_zf变为无符号int类型,如果插入减号,将得到错误。 Out of range value for column..... . 但是你可以把减号插入到int_10中。另外,如果您将4294967291插入到int_10中,您将得到错误信息。 列的超出范围值…..

    结论:

    1. 不带关键字的int(x) 零填充 , is equal to int range -2147483648~2147483647

    2. 带关键字的int(x) 零填充 ,该字段等于无符号int范围0~4294967295,如果num的长度小于x,则向左填充0。