MySQL | BOOLEAN
Start your free 7-days trial now!
MySQL inherently does not support native Boolean data types. However, MySQL still provides us with the datatypes: BOOLEAN
and BOOL
, which are just aliases for TINYINT
.
The unfortunate consequence of this mapping is that TINYINT
can take on a value within the interval of 0 to 255. This means that, if we are not careful, we can insert values like 5, which clearly is not desirable.
In MySQL, the keyword TRUE
evaluates to 1, while FALSE
evaluates to 0. When inserting values into the database, we can use the aliases TRUE
and FALSE
instead of messing around with numerical values.
Example
To create a table with a boolean column is_married
:
CREATE TABLE people (name VARCHAR(20), age INT, is_married BOOLEAN);
Query OK, 0 rows affected (0.04 sec)
To insert values for is_married
using TRUE
and FALSE
aliases:
INSERT INTO people (name, age, is_married) VALUES ("Alex", 30, TRUE);INSERT INTO people (name, age, is_married) VALUES ("Bob", 15, FALSE);
To check how the inserted values look:
SELECT * FROM people;
+------+------+------------+| name | age | is_married |+------+------+------------+| Alex | 30 | 1 || Bob | 15 | 0 |+------+------+------------+
Notice how the aliases TRUE
and FALSE
are automatically converted to 1
and 0
when stored.