How do i add a boolean column to a mysql table?

A Boolean is the simplest data type that always returns two possible values, either true or false. It can always use to get a confirmation in the form of YES or No value.

MySQL does not contain built-in Boolean or Bool data type. They provide a TINYINT data type instead of Boolean or Bool data types. MySQL considered value zero as false and non-zero value as true. If you want to use Boolean literals, use true or false that always evaluates to 0 and 1 value. The 0 and 1 represent the integer values.

Execute the following statement to see the integer values of Boolean literals:

After successful execution, the following result appears:

MySQL Boolean Example

We can store a Boolean value in the MySQL table as an integer data type. Let us create a table student that demonstrates the use of Boolean data type in MySQL:

In the above query, we can see that the pass field is defined as a Boolean when showing the definition of a table; it contains TINIINT as follows:


Let us add two new rows in the above table with the help of following query:

When the above query executed, immediately MySQL checks for the Boolean data type in the table. If the Boolean literals found, it will be converted into integer values 0 and 1. Execute the following query to get the data from the student table:

You will get the following output where the true and false literal gets converted into 0 and 1 value.

Since MySQL always use TINYINT as Boolean, we can also insert any integer values into the Boolean column. Execute the following statement:

You will get the following result:

In some cases, you need to get the result in true and false literals. In that case, you need to execute the if[] function with the select statement as follows:

It will give the following output:

MySQL Boolean Operators

MySQL also allows us to use operators with the Boolean data type. Execute the following query to get all the pass result of table student.

This statement returns the following output:

The above statement only returns the pass result if the value is equal to 1. We can fix it by using the IS operator. This operator validates the value with the Boolean value. The following statement explains this:

After executing this statement, you will get the following result:

If you want to see the pending result, use IS FALSE or IS NOT TRUE operator as below:

You will get the following output:

Summary: this tutorial shows you how to use MySQL BOOLEAN data type to store Boolean values, true and false.

MySQL does not have built-in Boolean type. However, it uses TINYINT[1] instead. To make it more convenient, MySQL provides BOOLEAN or BOOL as the synonym of TINYINT[1].

In MySQL, zero is considered as false, and non-zero value is considered as true. To use Boolean literals, you use the constants TRUE and FALSE that evaluate to 1 and 0 respectively. See the following example:

SELECT true, false, TRUE, FALSE, True, False; -- 1 0 1 0 1 0

Code language: SQL [Structured Query Language] [sql]

MySQL BOOLEAN example

MySQL stores Boolean value in the table as an integer. To demonstrate this, let’s look at the following tasks table:

CREATE TABLE tasks [ id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR[255] NOT NULL, completed BOOLEAN ];

Code language: SQL [Structured Query Language] [sql]

Even though we specified the completed column as BOOLEAN, when we show the table definition, it is TINYINT[1] as follows:

DESCRIBE tasks;

Code language: SQL [Structured Query Language] [sql]

The following statement inserts 2 rows into the tasks table:

INSERT INTO tasks[title,completed] VALUES['Master MySQL Boolean type',true], ['Design database table',false];

Code language: SQL [Structured Query Language] [sql]

Before saving data into the Boolean column, MySQL converts it into 1 or 0. The following query retrieves data from tasks table:

SELECT id, title, completed FROM tasks;

Code language: SQL [Structured Query Language] [sql]

As you see, the true and false were converted to 1 and 0.

Because Boolean is TINYINT[1], you can insert value other than 1 and 0 into the Boolean column. Consider the following example:

INSERT INTO tasks[title,completed] VALUES['Test Boolean with a number',2];

Code language: SQL [Structured Query Language] [sql]

It is working fine.

If you want to output the result as true and false, you can use the IF function as follows:

SELECT id, title, IF[completed, 'true', 'false'] completed FROM tasks;

Code language: SQL [Structured Query Language] [sql]

MySQL BOOLEAN operators

To get all completed tasks in the tasks table, you might come up with the following query:

SELECT id, title, completed FROM tasks WHERE completed = TRUE;

Code language: SQL [Structured Query Language] [sql]

As you see, it only returned the task with completed value 1. To fix it, you must use IS operator:

SELECT id, title, completed FROM tasks WHERE completed IS TRUE;

Code language: SQL [Structured Query Language] [sql]

In this example, we used the IS operator to test a value against a Boolean value.

To get the pending tasks, you use IS FALSE or IS NOT TRUE as follows:

SELECT id, title, completed FROM tasks WHERE completed IS NOT TRUE

Code language: SQL [Structured Query Language] [sql]

In this tutorial, you have learned how to use the MySQL BOOLEAN data type, which is the synonym of TINYINT[1], and how to manipulate Boolean values.

Was this tutorial helpful?

How do I create a new boolean column 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.

How do you add a boolean column?

ALTER TABLE table_name ALTER COLUMN col_name SET NOT NULL; Or you can put them all together in a single statement: ALTER TABLE table_name ADD COLUMN “col_name” BOOLEAN DEFAULT FALSE; This way it might take longer if the operation is huge.

How do you add a boolean value to a table?

Since MySQL always use TINYINT as Boolean, we can also insert any integer values into the Boolean column. Execute the following statement: Mysql> INSERT INTO student[name, pass] VALUES['Miller',2];

How do you create a boolean column in SQL?

In SQL Server, a Boolean Datatype can be created by means of keeping BIT datatype. Though it is a numeric datatype, it can accept either 0 or 1 or NULL values only. Hence easily we can assign FALSE values to 0 and TRUE values to 1. This will provide the boolean nature for a data type.

Can you store boolean in MySQL?

MySQL does not have a boolean [or bool] data type. Instead, it converts boolean values into integer data types [TINYINT]. When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

Does MySQL have a boolean data type?

MySQL does not provide a built-in Boolean data type. It uses TINYINT[1] instead which works the same.

Bài mới nhất

Chủ Đề