0510ML Projects and portfolio

16Apr/100

The meaning of int(size) in MySQL

I've sometimes been confused about the size in int(size) when creating a field in a MySQL table. I stumbled about a blog post of Alexander Kirk that explains the meaning:

Usually you see something like int(11) in CREATE TABLE statements, but you can also change it to int(4). So what does this size mean? Can you store higher values in a int(11) than in an int(4)?

[...]

It's about the display width. The weird thing is, though, that, for example, if you have a value of 5 digits in a field with a display width of 4 digits, the display width will not cut a digit off.

If the value has less digits than the display width, nothing happens either. So it seems like the display doesn't have any effect in real life.

Now ZEROFILL comes into play. It is a neat feature that pads values that are (here it comes) less than the specified display width with zeros, so that you will always receive a value of the specified length. This is for example useful for invoice ids.

So, concluding: The size is neither bits nor bytes. It's just the display width, that is used when the field has ZEROFILL specified.