MySQL Integer Types

Integer Types and Ranges


  • Unsigned Range 0 - 255
  • Signed Range -128 - 127
  • Storage Space: 1 byte


  • Unsigned Range 0 - 65535
  • Signed Range -32768 - 32767
  • Storage Space: 2 bytes


  • Unsigned Range 0 - 16777215
  • Signed Range -8388608 - 8388607
  • Storage Space: 3 bytes


  • Unsigned Range 0 - 4294967295
  • Signed Range -2147483648 - 2147483647
  • Storage Space: 4 bytes


  • Unsigned Range 0 - 18446744073709551615
  • Signed Range -9223372036854775808 - 9223372036854775807
  • Storage Space: 8 bytes

Length is Almost Pointless (For These Types)

In most table definitions I see, people specifying the length for an integer, for example:


The (3) part does not prevent the column from taking the value 1000 which has 4 digits. Also, because it is not specified as UNSIGNED, it will allow a negative value which is probably not desired. The maximum and minimum values the column can take are only defined by the type, not the length. In fact the length only really matters when the column also has the zerofill attribute, in which case 0's are prefixed to any value below that length when being displayed. This can be best demonstrated by the following code:

CREATE TABLE `mytable` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `int1` int(10) NOT NULL,
    `int2` int(3) NOT NULL,
    `zerofill1` int(10) ZEROFILL NOT NULL,
    `zerofill2` int(3) ZEROFILL NOT NULL,
    PRIMARY KEY (`id`)

INSERT INTO `mytable` 
(`int1`, `int2`, `zerofill1`, `zerofill2`) 
(10000, 10000, 10000, 10000),
(100, 100, 100, 100);

select * from mytable;

The output of which is below:

| id | int1  | int2  | zerofill1  | zerofill2 |
|  1 | 10000 | 10000 | 0000010000 |     10000 |
|  2 |   100 |   100 | 0000000100 |       100 |

As you can see, having a length of 3 didn't prevent a value of 10000 being used or displayed for the zerofill2 column which has zerofill and a length of just 3. Nor did it reduce the amount of padding/spacing for the column. The output shows a width of 10 digits in zerofill2 even though length is specified as just 3. This is because the INT type can take a value with 10 digits. Compare this with column int1 which has a much larger length of 10 but only has a width of just 5 characters because that is the largest number it has to display for this select query.


Please don't specify the length on integer types unless you want to prefix any number with fewer digits than that with 0s, and pay more attention to using the correct type and correct signage instead of giving everything just an int value.




Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus
We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites. More info.