In MySQL table definitions is there a different between INT(5)
and SMALLINT(5)
? Or do they both represent the same size?
1 Answer
An int
and a smallint
have different sizes and consequently ranges.
The (5)
is smallint(5)
or int(5)
is called a "Numeric Type Attribute" and it represents the "display width" of the field,
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 anINT
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.)When used in conjunction with the optional (nonstandard) attribute
ZEROFILL
, the default padding of spaces is replaced with zeros. For example, for a column declared asINT(4) ZEROFILL
, a value of5
is retrieved as0005
.
This is a MySQL "extension", and zerofill is an "extension" on an extension.
For the sane method of controlling the display of a numeric type, don't ues Numeric type Attributes and instead use
-
This attribute is mandatory in MySQL Workbench. What happens if I have the number 5000 and a data type
SMALLINT(1)
? Does the number 5000 get truncated to one digit? Which digit?– posfan12Dec 16, 2019 at 16:13 -
What is the name of this MySQL extension for display width, please? Sep 3, 2020 at 13:58