Mysql int fields not clipped

as you know, when you describe varchar or integer fields, you must set their length ...

something like int (5) or varchar (5) ...

but when trying to add 123456 to both fields .., while the varchar field truncates the value, the integer field does not truncate it ...

so what is the purpose of describing the length of an int?

+4
source share
5 answers

N in INT(N) indicating the display length of the application; was very misleading due to syntactic similarities with VARCHAR(N) and, of course, was often misunderstood. This is really pointless for all the applications I've seen.

This applies to all TINYINT , SMALLINT , MEDIUMINT , INT , BIGINT .

+3
source

int(5) does not do what you think it does: it sets an entire field with a display width of 5 digits, that is, digits shorter than 5 digits will be filled with spaces.

In MySQL, int values ​​are always 4 bytes and can go from -2,147,483,648 to 2,147,483,647.

See http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html .

+4
source

It seems that this length is used only for display: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

For example, INT (4) indicates an INT with a four-digit display width. This extra screen width can be used by applications to display integer values ​​that are less than the width specified for the column by adding spaces to the left. (That is, this width is present in the metadata returned by the result sets. Whether it is used or not before the application.)

+2
source

The size of type INT is neither bits nor bytes. This is simply the width of the screen that is used when the field has ZEROFILL.

See this blog article for a detailed explanation.

+2
source

FRom 10.2. Numeric types

MySQL supports the extension for optionally showing the width of the integer data types in parentheses after the base keyword for the type. For example, INT (4) indicates an INT with a four-digit display width. This optional width display can be used by applications to display integer values ​​that have a width less than the width specified for the column on the left, complementing their spaces. (That is, this width is equal to being present in the metadata returned using the result sets. Whether it is used or not depends on the application.)

Screen width does not limit the range of values ​​that can be stored in a column. It also does not prevent values ​​wider than displaying column widths from the correct display.

+2
source

All Articles