I have a column which always stores 5 digit value (eg: 10100, 60200, 30100 etc). I am in a confusion to which data-type should be good to use (char(5) or varchar(5) or smallInt). As I understand smallint only takes 2 bytes, how many bytes would char(5) and varchar(5) take? And which data-type would be better for my scenario in terms of both performance and storage?
Share
2 bytes of data = a number from 0 to 65,535 if unsigned or from -32,768 to 32,767 if signed. (unsigned= non-negative values, singed=range of negative to positive).
http://dev.mysql.com/doc/refman/4.1/en/integer-types.html
Using a medium int (0 to 16,777,215 unsigned, -8,388,608 to 8,388,607 signed) will guarantee accepted values of any 5 digit number and is what I suggest using, since it requries only 3 bytes of data versus a varchar/char of 5. If you must use characters, and you know that there will be no more than 5 or less than 5 characters, it’s good practice to use
charovervarchar.