Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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.

like image 293
A.C.Balaji Avatar asked Apr 06 '11 06:04

A.C.Balaji


People also ask

What is int data type in MySQL?

INT in MySQL is an integer data type, which is a whole number. It allows numbers to be written without fractional part (no decimal component). For example, 1, 5, -10 is an integer type, while 1.5, 5/7 cannot be an integer. It is to note that an integer value can be positive, negative, or zero.

What is difference between INT and INT () in MySQL?

There is no difference between the two except for a slight difference in storage, when we use data. In practice, to see the different effects of the two, you need to add the property of zerofill after the field type when creating the table, which means filling with 0, otherwise you will not see the effect.

What is the difference between INT and Smallint?

Both data types are stored as signed binary integers. INTEGER values have 32 bits and can represent whole numbers from –2 31–1 through 2 31–1. SMALLINT values have only 16 bits. They can represent whole numbers from –32,767 through 32,767.


2 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.

like image 200
WhiteFang34 Avatar answered Sep 30 '22 11:09

WhiteFang34


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

like image 37
Jon Black Avatar answered Sep 30 '22 10:09

Jon Black