MySQL Boolean Data Type
Somebody posted a quick question about the outcome of defining a table with a bool data type in PHPMyAdmin. They were subsequently surprised when they checked the MySQL database and found that it was actually a tinyint(1). The natural question they had was: “What do you enter – true/false or 1/0?”
I promised to post an answer tonight, and morning counts too. You can enter a true or false because they’re synonyms for a 1 or 0 respectively. TINYINT is the supported data type, and BIT, BOOL, and BOOLEAN are synonyms for the base data type.
Here’s an example in MySQL:
mysql> CREATE TABLE data_type (TYPE bool); mysql> DESCRIBE data_type; +-------+------------+------+-----+---------+-------+ | FIELD | TYPE | NULL | KEY | DEFAULT | Extra | +-------+------------+------+-----+---------+-------+ | TYPE | tinyint(1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ mysql> INSERT INTO data_type VALUES (TRUE),(FALSE); mysql> SELECT * FROM data_type; +------+ | TYPE | +------+ | 1 | | 0 | +------+ |
The comment below raises the question of what happens with values in the valid range of TINYINT that aren’t 0 or 1, like 5. The simple answer is they’re not valid when compared against the true and false constants, as you can see by creating the following example.
-- Create a test table. CREATE TABLE verify ( verify_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT , range_1 tinyint UNSIGNED , range_2 tinyint ); -- Insert test values. INSERT INTO verify VALUES (NULL, 0, 0) ,(NULL, 1, 1) ,(NULL, 1,-1) ,(NULL, 2, 2); -- Query results. SELECT range_1 AS "Value" , CASE WHEN range_1 = TRUE THEN 'True' WHEN range_1 = FALSE THEN 'False' ELSE 'Invalid' END AS "Unsigned" , range_2 AS "Value" , CASE WHEN range_2 = TRUE THEN 'True' WHEN range_2 = FALSE THEN 'False' ELSE 'Invalid' END AS "Signed" FROM verify; |
The results of the test demonstrate that only a 0 or 1 value validates against the false or true constants, as shown:
+-------+----------+-------+---------+ | Value | Unsigned | Value | Signed | +-------+----------+-------+---------+ | 0 | False | 0 | False | | 1 | True | 1 | True | | 1 | True | -1 | Invalid | | 2 | Invalid | 2 | Invalid | +-------+----------+-------+---------+ |
Based on the comment, the 256 value range of TINYINT is found here.
TINYINT(1) isn’t a synonym for bit(1).
TINYINT(1) can store -9 to 9.
TINYINT(1) UNSIGNED: 0-9
BIT(1): 0, 1. (Bit, literally).
Justin Rovang
25 Aug 11 at 4:24 pm
Excuse me, seems I’ve fallen victim to substr-ism:
TINYINT(1): -128-+127
TINYINT(1) UNSIGNED: 0-255
Justin Rovang
25 Aug 11 at 4:32 pm
True enough, but the post was about what PHPMyAdmin listed as a Boolean, and there it only uses 0 or 1 from the entire wide range of 256 possibilities.
maclochlainn
25 Aug 11 at 11:35 pm
What status of BOOL will return TINYINT value 5 for example?
If we presume that TRUE is 1 and FALSE is 0, value 5 will be not in range.
I know that BOOL value cannot be stored in 1 bit. Actually I don’t even know for any system which operates with lower than 8-bit words.
interesting
26 Aug 11 at 8:17 am
Actually, although using 1 is prefered in bool, it’s actually 0 for false and non-zero for true – so 5 would be same as 1, beeing both non-zero
Luís Rodrigues
22 Feb 12 at 9:26 am
of course as stated, the variables true and false will only be evaluated against the prefered values of 0 for false and 1 for true. If for some reason you store different values, you won’t be able to use those variables to evaluate the result – unless you only compare false – so if it’s 0 it’s false else it’s true
Luís Rodrigues
22 Feb 12 at 9:32 am
Luis, Excellent points about programming in general but in MySQL
TRUEis a constant with a value of 1 andFALSEis a constant with a value of 0. Try the test case provided in the blog post.maclochlainn
22 Feb 12 at 9:18 pm
[...] check this older post on how MySQL manages logical constants and the realities of TRUE and FALSE constants [...]
How to stripe views
28 May 12 at 9:38 am
thanks for the explaination
gigi
26 Sep 12 at 9:37 pm
Hi i want to add the true false button to mysql table , i have 10 quiz and i add this code to my sql as a column , how can i add the radio button pn php?
thanks
Celine
15 Apr 13 at 7:56 pm
Typically, you define a column with a
boolortinyintdata type, and you send the following from your PHP form:Selected radio button should send 1
Unselected radio button should send 0
Hope that helps.
maclochlainn
15 Apr 13 at 9:35 pm