Is BIGINT(8) the largest integer mysql can store?

前端 未结 5 1910
悲&欢浪女
悲&欢浪女 2020-12-09 14:50

I\'ve got some numbers that is now larger than INT can handle.

This is a bit embarassing, but I honestly don\'t know exactly what the BIGINT(8) means. Is the 8 in th

相关标签:
5条回答
  • 2020-12-09 15:10

    The number represents how it's displayed - it doesn't affect how the data is stored.

    From the manual:

    Another extension is supported by MySQL for optionally specifying the display width of integer data types in parentheses following the base keyword for the type (for example, INT(4)). 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. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

    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.

    A BIGINT is always 8 bytes and can store -9223372036854775808 to 9223372036854775807 (signed) or 0 to 18446744073709551615 (unsigned).

    0 讨论(0)
  • 2020-12-09 15:11

    The number between brackets is the 'display width' and is unrelated the range of numbers the datatype can actually store.

    A BIGINT's range is -9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615.

    You can find more information here: http://dev.mysql.com/doc/refman/5.4/en/numeric-type-overview.html

    0 讨论(0)
  • 2020-12-09 15:19

    You answer is in this overview. A BIGINT is indeed 8 bytes. A TinyInt only 1.

    Btw, I don't consider the range from -9223372036854775808 to 9223372036854775807 very embarrassing, it's +/-2^63 :).

    0 讨论(0)
  • 2020-12-09 15:24

    The number merely defines the width of the number when being displayed. Have a look at the mysql manual for sizes of numeric types.

    0 讨论(0)
  • 2020-12-09 15:30

    To store a 128-bit integer, you can use a BINARY(16).

    For 252:

    SELECT RIGHT(CONCAT(REPEAT("\0",16),UNHEX(CONV(252,10,16))),16);

    or equivalently:

    SELECT UNHEX(RIGHT(CONCAT(REPEAT("0",32),HEX(252)),32));

    (but MySQL integer calculations are 64-bit)

    See also the recent IPv6 functions INET6_ATON() and INET6_NTOA(), http://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function_inet6-aton and how they use a VARBINARY(16).

    0 讨论(0)
提交回复
热议问题