Difference between "int" and "int(2)" data types in my sql

81,968

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
81,968

Related videos on Youtube

A.C.Balaji
Author by

A.C.Balaji

PHP programmer

Updated on October 14, 2020

Comments

  • A.C.Balaji
    A.C.Balaji over 3 years

    Just I wonder why the range is given with the my sql 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.

    Thanks in advance.

  • A.C.Balaji
    A.C.Balaji about 13 years
    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
    A.C.Balaji about 13 years
    I got it, but the doubt is, for this purpose(display width) why don't we do that in programming language itself ?
  • WhiteFang34
    WhiteFang34 about 13 years
    That is a good question. It seems to only be there for programs like the command line client. Other programs can optionally use it.
  • A.C.Balaji
    A.C.Balaji about 13 years
    But for the types "varchar" and "char", it is mean to the range only,not just about the display width ?
  • WhiteFang34
    WhiteFang34 about 13 years
    Yeah 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.
  • Dmytro Dzyubak
    Dmytro Dzyubak over 9 years
    +1, exactly wanted to clarify the analogy between "CHAR" - "VARCHAR(N)" and "INT" - "INT(N)" - so these are not the same!