Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOL and tinyint(1) ... unsigned?

Tags:

mysql

boolean

i've read that the bool type in mysql is an alias of tinyint(1), therefore i should use tinyint

My question is the following: Do i need to declare it unsigned, i mean, is it necessary ?

purchased tinyint(1) unsigned not null DEFAULT 0,

or

purchased tinyint(1) not null DEFAULT 0,
like image 486
Marco Avatar asked Jul 07 '11 21:07

Marco


People also ask

Is Tinyint signed or unsigned?

SIGNED, UNSIGNED and ZEROFILL For example, a TINYINT UNSIGNED can range from 0 to 255. Floating point and fixed-point types also can be UNSIGNED , but this only prevents negative values from being stored and doesn't alter the range.

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 and boolean same?

There is no difference between TINYINT(1) and Boolean. The keyword Bool or Boolean internally converts into TINYINT(1) or we can say Bool or Boolean are synonymous with TINYINT(1).

How do you convert Tinyint to boolean?

While determining whether something is TRUE or FALSE, any int (int, tinyint, smallint, bigint) value converts to a boolean value. If it's 0, it's considered FALSE; if not, it's considered TRUE. So, 2 is also TRUE here. BOOL, BOOLEAN are synonyms for TINYINT(1).


2 Answers

It's not necessary; leave it signed. In fact, it doesn't matter anyway — 0 and 1 are within the range of valid values for TINYINT regardless of its signedness.

But, seriously, just declare it a BOOL, it makes it very clear that it's a true-or-false value.

like image 185
BoltClock Avatar answered Sep 28 '22 10:09

BoltClock


It is not necssary to declare it unsigned, particularly if you're using it to store a boolean value.

like image 34
Paul Sonier Avatar answered Sep 28 '22 11:09

Paul Sonier