gotchasqlMinor
Tinyint structure is confusing
Viewed 0 times
confusingtinyintstructure
Problem
I am confused to see this
tinyint(3) structure. If tinyint can store only 1 byte then what is the purpose of giving the size in brackets? Is there something special with this parameter in MySQL?Solution
That's not bytes, it's display width. It's used as a hint for the maximum expected typical character width of values for the column, to applications that care to actually use the metadata -- which many don't.
As explained in the documentation:
MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example,
The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as
The default value for an unsigned tinyint is (3) because that's the size of "255" which the widest number (in displayed character positions) that can be stored in an an unsigned tinyint.
The display width also does not change the storage requirements for the data types; so, for example, an INT(1) still takes up 4 bytes of storage.
As explained in the documentation:
MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example,
INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as
SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.The default value for an unsigned tinyint is (3) because that's the size of "255" which the widest number (in displayed character positions) that can be stored in an an unsigned tinyint.
The display width also does not change the storage requirements for the data types; so, for example, an INT(1) still takes up 4 bytes of storage.
Context
StackExchange Database Administrators Q#37195, answer score: 8
Revisions (0)
No revisions yet.