46

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

This is a bit embarrassing, but I honestly don't know exactly what the BIGINT(8) means. Is the 8 in that the max bit value or the max length?

So BIGINT(1) can only be one digit? Or is BIGINT(1) something else? I think TINYINT(1) max is 127, how does that work out?

  • What is the biggest I can make BIGINT?
  • What is the largest number I can store in MySQL as an integer?
informatik01
  • 16,038
  • 10
  • 74
  • 104
Citizen
  • 12,430
  • 26
  • 76
  • 117

5 Answers5

47

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).

Community
  • 1
  • 1
Greg
  • 316,276
  • 54
  • 369
  • 333
12

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 :).

Abel
  • 56,041
  • 24
  • 146
  • 247
6

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

soulmerge
  • 73,842
  • 19
  • 118
  • 155
5

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

In MySQL the BIGINT numeric data type requires 8 bytes for storage and has the following ranges:

  • The signed range is -9223372036854775808 to 9223372036854775807 (i.e. from -263 to 263-1).
  • The unsigned range is 0 to 18446744073709551615 (i.e. from 0 to 264-1).

You can find more information here: MySQL Numeric Data Types.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Andre Miller
  • 15,255
  • 6
  • 55
  • 53
3

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).