Home > Database > Mysql Tutorial > BOOLEAN or TINYINT to store values ​​in MySQL?

BOOLEAN or TINYINT to store values ​​in MySQL?

PHPz
Release: 2023-09-18 18:53:13
forward
1482 people have browsed it

BOOLEAN 或 TINYINT 在 MySQL 中存储值?

MySQL's BOOLEAN and BOOL are equivalent to TINYINT(1). Whenever you create a column using the BOOLEAN and BOOL data types, MySQL implicitly converts BOOLEAN and BOOL to TINYINT(1). BOOLEAN and BOOL are equivalents of TINYINT(1) because they are synonyms.

Create a table using the BOOLEAN data type. Query statement to create table.

mysql> create table BooleanDemo
   -> (
   -> IsOn BOOLEAN
   -> );
Query OK, 0 rows affected (0.58 sec)
Copy after login

Now check the internal structure of the above table. The query is as follows −

mysql> show create table BooleanDemo;
Copy after login

Output

+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                     |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
| BooleanDemo | CREATE TABLE `booleandemo` ( `IsOn` tinyint(1) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci    |
+-------------+----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
Copy after login

Looking at the above example output, BOOLEAN is converted to tinyint(1). The same is true for the BOOL data type. The query to create the table is as follows −

mysql> create table BOOLDemo
   -> (
   -> validUser BOOL
   -> );
Query OK, 0 rows affected (0.61 sec)
Copy after login

Now check the internal structure of the table. The query is as follows -

mysql> show create table BOOLDemo;
Copy after login

Output

+----------+------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                       |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
| BOOLDemo | CREATE TABLE `booldemo` (`validUser` tinyint(1) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci    |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of BOOLEAN or TINYINT to store values ​​in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template