Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Boolean "tinyint(1)" holds values up to 127?

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?

like image 939
JD Isaacks Avatar asked Dec 09 '10 18:12

JD Isaacks


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.

Is Tinyint Boolean in MySQL?

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.

Is 1 in MySQL True or false?

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.

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.


2 Answers

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.

like image 59
BoltClock Avatar answered Sep 20 '22 07:09

BoltClock


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).

like image 34
Nathan Avatar answered Sep 24 '22 07:09

Nathan