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...
The TINYINT takes 1 byte that means it has range -128 to +127 while int takes 4 bytes; it has range -2147483648 to +2147483647.
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.
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.
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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With