Sunday, December 20, 2009

MySQL Datatype & Length/Values

Here is explanation of what is meant by display width, taken directly from the source:

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.


So, if display width doesn’t constrain the range of values our numeric type can hold, why to use it at all? On MySQL forum, there was a post suggesting that by using the display width, we set mental reminder to ourselves on how big the field should be. Me personally, find this quite logical, this is some kind of mental tooltip which pops up when you review your schema.

Source from: PHP Magazine


Type Bytes Minimum Value Maximum Value


(Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127


0 255
SMALLINT 2 -32768 32767


0 65535
MEDIUMINT 3 -8388608 8388607


0 16777215
INT 4 -2147483648 2147483647


0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807


0 18446744073709551615

0 comments:

Post a Comment