Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean type on mysql

Tags:

sql

mysql

I'm looking for a way to store boolean data in SQL. I couldn't find a boolean type in MySQL. I looked at the table on w3schools (SQL Data Types) and didn't find a boolean type.

But I get the information that TINYINT(1) is used for boolean types.

Is it true, or is there another option?

like image 967
Harman met Avatar asked Jun 06 '16 21:06

Harman met


People also ask

Is 0 true or false in MySQL?

The constants TRUE and FALSE evaluate to 1 and 0 , respectively.

Is there a BOOLEAN data type in SQL?

There is no boolean data type in SQL Server. However, a common option is to use the BIT data type. A BIT data type is used to store bit values from 1 to 64. So, a BIT field can be used for booleans, providing 1 for TRUE and 0 for FALSE.

How do I add a boolean field in MySQL?

ALTER TABLE users ADD bio VARCHAR(100) NOT NULL; Adding a boolean column with a default value: ALTER TABLE users ADD active BOOLEAN DEFAULT TRUE; MySQL offers extensive documentation on supported datatypes in their documentation.

What is the datatype of boolean?

The BOOLEAN data type is a 1-byte data type. The legal values for Boolean are true ('t'), false ('f'), or NULL. The values are not case sensitive.


1 Answers

You can use BIT data type to store boolean data (like on T-SQL / SQL Server):

CREATE TABLE `table_name` (
    `column_name` BIT(1)
);

On MySQL the data types BOOL and BOOLEAN are also available:

CREATE TABLE `table_name` (
    `column_name1` BOOL,
    `column_name2` BOOLEAN
);

The BOOL and BOOLEAN data types are synonyms for TINYINT(1):

These types (BOOL and BOOLEAN) are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true.

like image 179
Sebastian Brosch Avatar answered Sep 27 '22 19:09

Sebastian Brosch