
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I add a Boolean field to MySQL?
You can use tinyint(1) or bool or boolean. All are synonym. If you use bool or boolean datatype, then it nternally change into tinyint(1).
In PHP, the value 0 represents false and 1 represents true. Any other number except 0 is also true.
Let us check the internal representation of bool or boolean using a table. The query to create a table is as follows.
mysql> create table AddBoolDemo -> ( -> isToggle bool -> ); Query OK, 0 rows affected (1.24 sec)
To check the DDL of the table, the following is the query.
SHOW CREATE TABLE yourTableName;
Let us check the representation of bool which internally converts into tinyint(1). Now check for the table AddBoolDemo.
mysql> show create table AddBoolDemo\G
The following is the output.
*************************** 1. row *************************** Table: AddBoolDemo Create Table: CREATE TABLE `addbooldemo` ( `isToggle` tinyint(1) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec)
Add true or false or 1 or 0 literals in the table using insert command.
mysql> insert into AddBoolDemo values(true); Query OK, 1 row affected (0.19 sec) mysql> insert into AddBoolDemo values(false); Query OK, 1 row affected (0.19 sec) mysql> insert into AddBoolDemo values(1); Query OK, 1 row affected (0.10 sec) mysql> insert into AddBoolDemo values(0); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement. The query is as follows.
mysql> select *from AddBoolDemo;
The following is the output.
+----------+ | isToggle | +----------+ | 1 | | 0 | | 1 | | 0 | +----------+ 4 rows in set (0.00 sec)
- Related Articles
- How can I add one day to DATETIME field in MySQL query?
- How can I update the boolean values in MySQL?
- How can I convert a string to boolean in JavaScript?
- How to insert a boolean field in MongoDB?
- Count boolean field values within a single MySQL query?
- How to add a day to datetime field in MySQL query?
- How do I add a field to existing record in MongoDB?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- MySQL query to fetch only a single field on the basis of boolean value in another field
- How can I create a MySQL boolean column and assign value 1 while altering the same column?
- Is there a way in MySQL to reverse a boolean field with a single query?
- How can we enter BOOLEAN values in MySQL statement?
- How can I add items to a spinner in Android?
- Add a single day to datetime field with MySQL INTERVAL
- How to update field to add value to existing value in MySQL?

Advertisements