32

In MySQL table definitions is there a different between INT(5) and SMALLINT(5)? Or do they both represent the same size?

0

1 Answer 1

43

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 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.)

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 as INT(4) ZEROFILL, a value of 5 is retrieved as 0005.

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

2
  • 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?
    – posfan12
    Dec 16, 2019 at 16:13
  • What is the name of this MySQL extension for display width, please? Sep 3, 2020 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.