A.C.Balaji
A.C.Balaji

Reputation: 1113

Difference between "int" and "int(2)" data types

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.

Upvotes: 49

Views: 99214

Answers (5)

theMyth
theMyth

Reputation: 278

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

Table description

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;

Shows display width results

I have also created this table which shows all the data types, min, max and their display width:

Int data types, display width and numbers table

Upvotes: 1

Sergey Podgornyy
Sergey Podgornyy

Reputation: 718

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.

Upvotes: 0

WhiteFang34
WhiteFang34

Reputation: 72039

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.

Upvotes: 56

Jon Black
Jon Black

Reputation: 16559

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

Upvotes: 22

Ciaran Keating
Ciaran Keating

Reputation: 2783

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.

Upvotes: 9

Related Questions