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

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.


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.

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.


drop table if exists foo;
create table foo
bar smallint(4) unsigned zerofill not null default 0

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


create table users
user_id int unsigned not null auto_increment primary key, -- 0 to 4294967295

So, which is it to be - unsigned or signed ??

Hope this helps :)

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.

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.

Need Your Help

How can I find all hard coded strings in my project in Android Studio

android internationalization android-studio

I need to find and extract all hard coded strings in my project in Android Studio (beta) 0.84.