BOOL and tinyint(1) ... unsigned?
i've read that the bool 开发者_StackOverflow中文版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,
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.
It is not necssary to declare it unsigned, particularly if you're using it to store a boolean value.
My two cents. As of today (2022-11-09) there are two answers to this question telling that it is "not necessary" to define it as unsigned. But my understanding is that it is not just "not necessary", it should be avoided. MySQL and MariaDB docs say that BOOLEAN is a synonym of TINYINT(1), not of TINYINT(1) UNSIGNED. And it seems that this fact is widely used by client libraries and ORMs. TINYINT(1) being read as boolean and TINYINT(1) UNSIGNED as a number.
Concrete example: https://mysqlconnector.net/api/mysqlconnector/mysqlconnectionstringbuilder/treattinyasboolean/
精彩评论