Types in MySQL: BigInt(20) vs Int(20)

前端 未结 6 1346
野性不改
野性不改 2020-11-22 16:15

I was wondering what the difference between BigInt, MediumInt, and Int are... it would seem obvious that they would allow for larger n

相关标签:
6条回答
  • 2020-11-22 16:29

    The number in parentheses in a type declaration is display width, which is unrelated to the range of values that can be stored in a data type. Just because you can declare Int(20) does not mean you can store values up to 10^20 in it:

    [...] 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. ...

    The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range allowed by three characters are displayed using more than three characters.

    For a list of the maximum and minimum values that can be stored in each MySQL datatype, see here.

    0 讨论(0)
  • 2020-11-22 16:31

    Quote:

    The "BIGINT(20)" specification isn't a digit limit. It just means that when the data is displayed, if it uses less than 20 digits it will be left-padded with zeros. 2^64 is the hard limit for the BIGINT type, and has 20 digits itself, hence BIGINT(20) just means everything less than 10^20 will be left-padded with spaces on display.

    0 讨论(0)
  • 2020-11-22 16:32

    Let's give an example for int(10) one with zerofill keyword, one not, the table likes that:

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

    Let's insert some data:

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

    Then

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

    We can see that

    • with keyword zerofill, num less than 10 will fill 0, but without zerofill it won't

    • Secondly with keyword zerofill, int_10_with_zf becomes unsigned int type, if you insert a minus you will get error Out of range value for column...... But you can insert minus to int_10. Also if you insert 4294967291 to int_10 you will get error Out of range value for column.....

    Conclusion:

    1. int(X) without keyword zerofill, is equal to int range -2147483648~2147483647

    2. int(X) with keyword zerofill, the field is equal to unsigned int range 0~4294967295, if num's length is less than X it will fill 0 to the left

    0 讨论(0)
  • 2020-11-22 16:39

    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) and BIGINT(20). It is advisable to store in BIGINT.

    0 讨论(0)
  • 2020-11-22 16:47

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

    • INT is a four-byte signed integer.

    • BIGINT is an eight-byte signed integer.

    They each accept no more and no fewer values than can be stored in their respective number of bytes. That means 232 values in an INT and 264 values in a BIGINT.

    The 20 in INT(20) and BIGINT(20) means almost nothing. It's a hint for display width. It has nothing to do with storage, nor the range of values that column will accept.

    Practically, it affects only the ZEROFILL option:

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

    It's a common source of confusion for MySQL users to see INT(20) and assume it's a size limit, something analogous to CHAR(20). This is not the case.

    0 讨论(0)
  • 2020-11-22 16:49

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

    In examples I'll use 401421228216, which is 101110101110110100100011101100010111000 (length 39 characters)

    • If you have INT(20) for system this means allocate in memory minimum 20 bits. But if you'll insert value that bigger than 2^20, it will be stored successfully, only if it's less then INT(32) -> 2147483647 (or 2 * INT(32) -> 4294967295 for UNSIGNED)

    Example:

    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)
    
    • If you have BIGINT(20) for system this means allocate in memory minimum 20 bits. But if you'll insert value that bigger than 2^20, it will be stored successfully, if it's less then BIGINT(64) -> 9223372036854775807 (or 2 * BIGINT(64) -> 18446744073709551615 for UNSIGNED)

    Example:

    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)
    
    0 讨论(0)
提交回复
热议问题