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

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.

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)

Confuse about the size in mysql table fields

I just created one role table and i defined role_id tinyint(1), even though i was able to store 99, 200 max upto 255 as tinyint limit for unsigned. so then what is the meaning of passing this size in data-types?
when read little that, it is about the display width, but when i fetch result on my php page, it displays as they are stored in the table.
Any mentor, can you please guide me to clear it?
from 10.1.1. Numeric Type Overview:
M indicates the maximum display width for integer types. The maximum legal
display width is 255. Display width is unrelated to the range of values a type
can contain [...].
So tinyint(M), or tinyint(1) in your case, means that any value stored in role_id will be chopped to a length of one byte when being displayed by MySQL. Similarly tinyint(3) would case a one digit integer to be padded with two spaces. You can also use tinyint(3) zerofill to pad with 0s instead, but it has the side-effect of making your tinyints unsigned.
However, regardless how you specify your tinyints they will be stored internally as an integer in the range [-128,127] (signed) or [0,255] (unsigned), and so that's the value PHP will receive when querying your database.
You can use mysql_field_len and substr to limit the display width similarly with PHP.

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.

MySQL int definition explanation

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

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