
- 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
What is the smallest datatype for one bit in MySQL?
The smallest datatype for one bit can be bit(1). The syntax is as follows −
yourColumnName bit(1)
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table bitDemo -> ( -> isValid bit(1) -> ); Query OK, 0 rows affected (0.49 sec)
Now you can check all the details of table with the help of SHOW CREATE command. The query is as follows −
mysql> show create table bitDemo;
Here is the output −
+---------+-----------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+-----------------------------------------------------------------------------------------------------------------------------+ | bitDemo | CREATE TABLE `bitdemo` (`isValid` bit(1) DEFAULT NULL) ENGINE =InnoDB DEFAULT CHARSET =utf8mb4 COLLATE =utf8mb4_0900_ai_ci | +---------+-----------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into bitDemo values(0); Query OK, 1 row affected (0.13 sec) mysql> insert into bitDemo values(1); Query OK, 1 row affected (0.10 sec) mysql> insert into bitDemo values(1); Query OK, 1 row affected (0.07 sec) mysql> insert into bitDemo values(0); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from bitDemo;
Here is the output −
+---------+ | isValid | +---------+ | | | | | | | | +---------+ 4 rows in set (0.00 sec)
To show the bit value, use the following query −
mysql> select isValid+0 from bitDemo;
The following is the output −
+-----------+ | isValid+0 | +-----------+ | 0 | | 1 | | 1 | | 0 | +-----------+ 4 rows in set (0.00 sec)
- Related Articles
- What is the best datatype for currencies in MySQL?
- For timestamp, which datatype is used in MySQL?
- What is the MySQL datatype to store DATALINK object in JDBC
- What is the usage of ZEROFILL for INT datatype?
- Which datatype should I use for flag in MySQL?
- What is the difference between BIT and TINYINT in MySQL?
- Which MySQL Datatype should be used for storing BloodType?
- How to set NOW() as default value for datetime datatype in MySQL?
- How to sum rows of VARCHAR datatype or TIME datatype in MySQL?
- Using Time datatype in MySQL without seconds?
- How to query JSON datatype in MySQL?
- In MySQL, what is Bit-field notation and how it can be used to write bit-field value?
- What is the smallest number?
- What is Parity Bit?
- How to get the datatype of MySQL table columns?

Advertisements