
MySQL Boolean
The MySQL BOOLEAN data type represents true or false values. The BOOLEAN data type can hold two possible values: TRUE and FALSE. It is often used to store binary or logical data, and it is synonymous with TINYINT(1).
Here’s how you can define a BOOLEAN column in a table:
CREATE TABLE my_table (
my_column BOOLEAN
);
You can insert data into a BOOLEAN column using 0 for FALSE and 1 for TRUE, like this:
INSERT INTO my_table (my_column) VALUES (1); -- TRUE
INSERT INTO my_table (my_column) VALUES (0); -- FALSE
When you query data from a BOOLEAN column, the values returned will be displayed as 1 for TRUE and 0 for FALSE.
SELECT my_column FROM my_table;
-- Result:
-- my_column
-- 1
-- 0
You can also use BOOLEAN data types in your SQL expressions to evaluate conditions or filter data based on true or false values. For example:
SELECT *
FROM my_table
WHERE my_column = 1; -- Retrieve rows where my_column is TRUE
In practice, BOOLEAN is often used to represent binary states or logical conditions in a database. It can be especially useful for scenarios where you need to store true/false, yes/no, or on/off values. Keep in mind that BOOLEAN values are often represented as 1 (TRUE) and 0 (FALSE) in MySQL.