Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference in using INT(1) vs TINYINT(1) in MySQL?

I'm under the assumption that INT(1) is the exact same thing as TINYINT(1) but I really have no idea. Whenever I've had values that can only be a single integer (e.g. a value 0-9), I've always just used INT(1) to say it's an integer and it will only be one character, which I assume means that it could only be a value 0 through 9 (please explain this to me if I'm wrong). I've always just ignored the other types of INT that you can cast the number as. I'm no MySQL savvy and tend to avoid the more complicated things you can do with it.

So my question, is there any difference between the various integer types INT, TINYINT, SMALLINT, MEDIUMINT, and BIGINT if you define a length of 1 for each type;? If not, should I use them anyways (I can see using them for more semantic meaning, TINYINT being more specific than just INT)? If so, could I easily (and/or should I) just go through my database and change all my INT(1) fields to TINYINT(1) fields?

like image 205
animuson Avatar asked Nov 08 '11 02:11

animuson


People also ask

When should I use Tinyint in MySQL?

TINYINT is a very small integer. The minimum and maximum SIGNED values are -128 and 127 respectively, while for UNSIGNED values TINYINT range is from 0 to 255. TINYINT uses 1 byte per row. It is the best option when you want to save space on your disk and enhance performance.

What does Tinyint mean in MySQL?

These are different data types, INT is 4-byte number, TINYINT is 1-byte number. More information here - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT. The syntax of TINYINT data type is TINYINT(M), where M indicates the maximum display width (used only if your MySQL client supports it). Numeric Type Attributes.

What is difference between Tinyint and boolean in MySQL?

The basic difference between Boolean and tinyint(1) is only in the naming convention. If we say that we need true or false values then Boolean comes to our mind, instead of tinyint(1). These data types are synonyms. It is up to us which data type we want to use- values can be 1 and 0 or true and false.


1 Answers

Here you'll understand it in a better way!

tinyint: 1 byte, -128 to +127 / 0 to 255 (unsigned) smallint: 2 bytes, -32,768 to +32,767 / 0 to 65,535 (unsigned) mediumint: 3 bytes, -8,388,608 to 8,388,607 / 0 to 16,777,215 (unsigned) int/integer: 4 bytes, -2,147,483,648 to +2,147,483,647 / 0 to 4,294,967,295 (unsigned) bigint: 8 bytes, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 / 0 to 18,446,744,073,709,551,615 (unsigned) 
like image 150
Cody Avatar answered Sep 19 '22 18:09

Cody