Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tinyint or varchar(1)?

Tags:

types

mysql

I have some fields in some tables that can contain only value from 0 to 9, and other that can contain 0 or 1.

For your experience, is it better to store it with TINYINT or VARCHAR(1) ?

As memory usage, speed to recognize them, and so on...

like image 881
markzzz Avatar asked Apr 23 '11 15:04

markzzz


People also ask

What does Tinyint 1 mean?

The TINYINT takes 1 byte that means it has range -128 to +127 while int takes 4 bytes; it has range -2147483648 to +2147483647.

What does Tinyint 2 mean?

The number 2 and 1 in TINYINT(2) vs TINYINT(1) indicate the display width. There is no difference between tinyint(1) and tinyint(2) except the width. If you use tinyint(2) or even tinyint(1), the difference is the same. You can understand the above concept using zerofill option.

What is Tinyint data type 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.

When should I use Tinyint in mysql?

If you include 2 or 3, you have to use tinyint (at the very smallest scale). "For example, it's better to use tinyint when you know that the only data you will store is a 1, 0 or null (with a very small chance of expanding that to a 2 or 3 later)." I'd use an ENUM for such a thing.


1 Answers

It depends on how you are going to use the values: as strings or as numbers. If you're going to use them as numbers, TINYINT(1) is definitely better as mysql won't need to do unnecessary conversions. For 1-character strings you could use CHAR(1).

like image 127
Eugene Yarmash Avatar answered Oct 04 '22 02:10

Eugene Yarmash