MySQL int definition explanation - mysql

For years I have been under the assumption when I create a new column of type bigint(12) for a MySQL table that the field is limited to integers with up to 12 digits. However, I recently noticed that values up to 16 digits are able to be written into and selected out of a bigint(12) defined column without any warnings or issues.
Can someone please help me understand why this is the case and what that column definition actually means? Thanks in advance!

bigint(12) is not truncated at all. The 12 is used for display purposes.
Have a look at Numeric Types
Numeric Type Attributes
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.

read Numeric Type Attributes section from MySql documentation INT(4) specifies an INT with a display width of four digits

Related

MYSQL - Warning: #1681 Integer display width is deprecated

I'm getting this warning when importing mysql dumps in phpMyAdmin:
Warning: #1681 Integer display width is deprecated and will be removed in a future release.
I found this on https://dev.mysql.com/worklog/task/?id=13127
Deprecate the ZEROFILL attribute for numeric data types and the display width attribute for integer types.
but i don't really understand what it means. Can someone explain what is the problem generating this warning, and how to resolve it.
Check this Numeric Type Attributes for the much complete story:
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 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.
So it shall be safe to ignore these kind of warning up to current version of MySQL (8.0.17 as of writing).
If you'd like to avoid these warnings and play safe, update all your affected tables having column type definitions of something like INT(##) to INT (i.e. without explicitly specifying the display width).
It means you should not specify the width of an integer value. Just write it as only int, not int(5).
The warning applies to all INT types: INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT. It means in future MySQL releases there will be no need to specify display length.
In my case it was TINYINT(1) that triggered the warning.
To prevent the warning, do:
is_duplicate TINYINT instead of is_duplicate TINYINT(1)

What's the purpose of the attribute in parentheses on ints in MySQL?

According to the documentation, the attribute in the parentheses (e.g. the "3" in SMALLINT(3)) is supposed to constrain the display of the values of the SMALLINT to three digits. However, when I inserted five test numbers (9, 99, 999, 9999, 99999), it took each number at its full value rather than capping it at the three digits and displayed it accordingly (except for 99999, of course, which it capped at 32767). Shouldn't the numbers be limited, at least while being displayed, to three digits? Or am I missing the true purpose here?
For integer types, the attribute relates to display width and can only be seen if you add zerofill to the column create statement.
Display width does not effect the storage allocated to the column, only the padded display when zerofill is enabled.
See also this explanation: http://matthom.com/archive/2006/11/28/mysql-integer-columns-and-display-width
As stated in the manual:
M indicates the maximum display width for integer types. For floating-point and fixed-point types, M is the total number of digits that can be stored (the precision). For string types, M is the maximum length. The maximum permissible value of M depends on the data type.
Further, as stated here:
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.
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.
Note
The ZEROFILL attribute is ignored when a column is involved in expressions or UNION queries.
If you store values larger than the display width in an integer column that has the ZEROFILL attribute, you may experience problems when MySQL generates temporary tables for some complicated joins. In these cases, MySQL assumes that the data values fit within the column display width.
All integer types can have an optional (nonstandard) attribute UNSIGNED. Unsigned type can be used to permit only nonnegative numbers in a column or when you need a larger upper numeric range for the column. For example, if an INT column is UNSIGNED, the size of the column's range is the same but its endpoints shift from -2147483648 and 2147483647 up to 0 and 4294967295.
Floating-point and fixed-point types also can be UNSIGNED. As with integer types, this attribute prevents negative values from being stored in the column. Unlike the integer types, the upper range of column values remains the same.
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
Integer or floating-point data types can have the additional attribute AUTO_INCREMENT. When you insert a value of NULL (recommended) or 0 into an indexed AUTO_INCREMENT column, the column is set to the next sequence value. Typically this is value+ 1, where value is the largest value for the column currently in the table. AUTO_INCREMENT sequences begin with 1.

mySQL datatypes - why is there a number after int: int(11)

According to the mySQL Docs a datatype of int (signed) has a range of -2147483648 to 2147483647. When I create a table with phpMyAdmin, and export the table structure it shows the following:
`unit_id` int(11) NOT NULL
Why the (11)? Doesn't int tell mySQL everything it needs to know?
This is the optional 'width' attribute, which can be used by applications to display the value.
To quote 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.
That sets the display width which is returned with the result set. It has no bearing on the storage size.

What does the number in parenthesis really mean?

I always thought that the number in the parenthesis represented the field length?
However, I understand that is not always the case. Maybe it's a MySQL issue? Someone told me if I set a field to 9 characters long, I can add a value that's more than 9 characters but only the first 9 will be saved.
Example:
CREATE TABLE `person` (
id INT,
age INT(2)
);
If that's the case, shouldn't I select something like TINYINT instead of INT for age?
INT(2) will generate an INT with the minimum display width of 2:
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.
this does not affect the range of possible values that can be stored in the field; neither is it the number of bytes used to store it. It seems to be only a recommendation for applications how to show the value, unless ZEROFILL is used (see the linked page).
A unsigned TINYINT (0...255) would probably do as well, unless cryopreservation takes a big step forward during the lifetime of your application.
That's the case for field types like vchar, but for numbers it represents the number of bytes which it uses to represent the number. An integer of two bytes means you can hold a number 2^16 - 1 (8 bits in a byte, so 16 total). If it's age, I figure you ought to be safe with two bytes. ;)

mysql phpmyadmin accepting 4 character but its real length is int 2

i have employee table and its empid length is INT 3 ,
But it accepting more then 3 character ....
How it is possible
(THIS IS TESTED IN PHPMYADMIN)
To cite the documentation: 10.2. Numeric Types
Another extension is supported by MySQL for optionally specifying the display width of integer data types in parentheses following the base keyword for the type (for example, INT(4)). 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.)
and
The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column
So you see, it is not a restriction that influences the data stored in MySQL but an additional information for applications that retrieve the data.