I wanted to make a true/false field for if an item is in stock.
I wanted to set it to Boolean ( which gets converted to tinyint(1)
), 1 for in stock, 0 for not in stock.
I am getting feeds from vendors, so I thought to myself, "What if they pass how many are instock?"
So I wondered if I inserted a number higher than 1 what would happen. I assumed it would default to 1.
To my surprise it will allow me to hold any number up to 127, anything over defaults to 127.
Can anyone explain why?
The TINYINT takes 1 byte that means it has range -128 to +127 while int takes 4 bytes; it has range -2147483648 to +2147483647.
In MySQL, TINYINT(1) and boolean are synonymous. Because of this, the MySQL driver implicitly converts the TINYINT(1) fields to boolean if the the Java tinyInt1isBit configuration property is set to true (which is the default) and the storage size is 1.
MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.
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.
The signed TINYINT
data type can store integer values between -128 and 127.
However, TINYINT(1)
does not change the minimum or maximum value it can store. It just says to display only one digit when values of that type are printed as output.
The tinyint
data type utilizes 1 byte of storage. 256 possible integer values can be stored using 1 byte (-128 through 127). if you define as tinyint unsigned
then negative values are discarded so is possible to store (0 through 255).
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