When we create a table and define an integer data type , we may define a specific length. For example:
SMALLINT(5)
,INT(11)
, eitherBIGINT(20)
Why is this length defined in an integer data type?
When we create a table and define an integer data type , we may define a specific length. For example:
SMALLINT(5)
,INT(11)
, eitherBIGINT(20)
Why is this length defined in an integer data type?
Investigating I found this documentation which gives a fairly clear idea, testing I was able to demonstrate what it is for, and the length that is defined in parentheses is called an attribute .
When declaring the integer data type, the attribute in question is used to define the fill length of the digit "0" on the left when doing a SELECT, and this can only be seen when you define the column with the ZEROFILL option. .
In the following example I create a table in the test database called demo, we are going to try with the TINYINT data type and show how it is created by default by MySQL, then values are inserted and we do an ALTER to see the changes:
I insert three values of different lengths:
Now I do a SELECT to see what's inside:
I modify the column so that it has the ZEROFILL property.
I do the SELECT again to see the change:
As we can see, it fills the integer value with three zeros "0" to the left. We can conclude that the attribute of the integers with the ZEROFILL property is used for a visual aspect.