Just I wonder why the range is given with the MySQL data types. I define a table with a field name "id" and the data type is "int(2)". I inserted the value to the field "id" as "123456". This is accepted and stored. So what is the use of giving the range.
-
1Here some info: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html – emco Apr 06 '11 at 06:42
-
Does this answer your question? [What does INT(5) in mysql mean?](https://stackoverflow.com/questions/8892341/what-does-int5-in-mysql-mean) – Nico Haase Aug 06 '23 at 13:44
5 Answers
For INT
and other numeric types that attribute only specifies the display width.
See Numeric Type Attributes in the MySQL 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.

- 4,549
- 3
- 24
- 39

- 70,765
- 18
- 106
- 111
-
Dear WhiteFang34, Thanks for your reply. I found some data in your given link. Still I couldn't get the reason of giving the range. Could you please clear me. – A.C.Balaji Apr 06 '11 at 06:51
-
I got it, but the doubt is, for this purpose(display width) why don't we do that in programming language itself ? – A.C.Balaji Apr 06 '11 at 06:56
-
That is a good question. It seems to only be there for programs like the command line client. Other programs can optionally use it. – WhiteFang34 Apr 06 '11 at 06:59
-
2But for the types "varchar" and "char", it is mean to the range only,not just about the display width ? – A.C.Balaji Apr 06 '11 at 07:05
-
2Yeah for `VARCHAR` and `CHAR` it specifies the length. For `DECIMAL` it does actually affect the precision before and after the decimal point. It's not always used the same. – WhiteFang34 Apr 06 '11 at 07:08
-
+1, exactly wanted to clarify the analogy between "CHAR" - "VARCHAR(N)" and "INT" - "INT(N)" - so these are not the same! – Dmytro Dzyubak Aug 25 '14 at 15:07
The optional display width specifier (for integer data types) is only applicable when using zerofill and has nothing to do with the internal size (in bytes) of the integer data type.
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
drop table if exists foo;
create table foo
(
bar smallint(4) unsigned zerofill not null default 0
)engine=innodb;
insert into foo (bar) values (781),(727);
select * from foo;
+-----------+
| bar |
+-----------+
| 0781 |
| 0727 |
+-----------+
More importantly, what you should be thinking about is whether your integer data types should be signed or unsigned e.g.
create table users
(
user_id int not null auto_increment primary key, -- -2147483648 to 2147483647
...
)engine=innodb;
vs.
create table users
(
user_id int unsigned not null auto_increment primary key, -- 0 to 4294967295
...
)engine=innodb;
So, which is it to be - unsigned or signed ??
Hope this helps :)

- 16,223
- 5
- 43
- 42
The (2) doesn't define the size of the integer. It's just number of digits to display in some tools - I'm not sure of the details. The size of an integer is determined by whether it's INT, TINYINT, SMALLINT, MEDIUMINT, or BIGINT.

- 2,793
- 21
- 19
I know there are quiet a lot of answers, but some of them are not entirely accurate so I just wanted to add a detailed explanation for this as I was learning about this myself.
The example is with MariaDB, but the same applies to MySQL
INT(m) where m is the display width in digits. But this only applies to zerofill.
I am using an example of INT(4) as its more clear than INT(2) which is small.
This only applies to zerofill where it pads up 0's to the left up to the specified display digit or the max number of digits that type can hold based on how much memory is allocated to it.
INT4 has a display width of 11 for signed & 10 for unsigned types because INT4 takes 4 bytes of memory and the maximum decimal number that can be contained in 4 bytes of memory is 10 digits, the 11th is reserved for the minus sign.
Here is a simple example, I have created a table with some columns and inserted some rows. It will also show how the default display width is added.
I am creating 2 zerofill columns, one with no display width specified (should be 10 as its unsigned), and another where I specify the display with of 5 (half) :
CREATE TABLE IntDisplayWidthEg(
normalInt INT,
normalIntWithDisplayWidth INT(2),-- Irrelevant
normalIntZeroFill INT ZEROFILL,
normalIntZeroFill5 INT(5) ZEROFILL
);
As you can see, it automatically adds the display width for INT data types, so you don't need to specify it. I have specifically created an irrelevant column with INT(2) to drive the point home.
The two columns below is where, you will see that ZEROFILL becomes UNSIGNED in newer versions of MariaDB.
When no display was specified, it automatically created the default display of 10 and I have created a display column with zerofill with INT(5)
Now I add some values and see the results:
INSERT INTO IntDisplayWidthEg(
normalInt, normalIntWithDisplayWidth, normalIntZeroFill, normalIntZeroFill5
)
VALUES
(10,2354, 9, 9),
(10,345748, 999, 9847334),
(18, 65, 9934, 924);
Results:
SELECT * FROM IntDisplayWidthEg;
I have also created this table which shows all the data types, min, max and their display width:

- 121
- 6
-
Why do you mention MariaDB in your answer? The question is tagged with MySQL – Nico Haase Mar 29 '23 at 07:21
-
Oh crap sorry, I didn't realise, I can change it, MariaDB is fully compatible with MySQL so the same should apply. I will edit it – theMyth Mar 29 '23 at 07:23
-
"MariaDB is fully compatible with MySQL" - are you sure about that? – Nico Haase Mar 29 '23 at 07:25
-
Not absolutely sure, I will need to look into it, but sure enough for the purpose of this question. Since Oracle bought MySQL, I am using MariaDB, it probably has some enhancements I will need to research, here is something: https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/#:~:text=MariaDB's%20data%20files%20are%20generally,frm)%20files%20are%20binary%20compatible. – theMyth Mar 29 '23 at 07:27
As far as I know, when you define INT(2)
(where maximum value is 4), you are allocating in memory 2 bits, but if you insert value, which is bigger, then 4, MySQL will request more memory from system.
So INT(2)
means allocate at least 2 bits for storing this value, if value bigger, then you specified (INT(2)
), DBMS will request memory ones again to request this memory. This is no good for performance reason to request memory twice, that's why better to allocate for column more space.

- 663
- 10
- 19
-
1Hey, i am sorry but this is not correct, I have provided a detailed explanation above – theMyth Mar 29 '23 at 07:33
-
1@theMyth yes, you are right, I found this in the doc too - https://dev.mysql.com/doc/refman/5.7/en/numeric-type-attributes.html Thanks for pointing that out – Sergey Podgornyy Mar 29 '23 at 09:41