BOOLEAN或TINYINT将值存储在MySQL中?
MySQLBOOLEAN和BOOL都等效于TINYINT(1)。每当使用BOOLEAN和BOOL数据类型创建列时,MySQL都会将BOOLEAN和BOOL隐式转换为TINYINT(1)。BOOLEAN和BOOL是TINYINT(1)的等效项,因为它们是同义词。
使用BOOLEAN数据类型创建一个表。查询创建表。
mysql> create table BooleanDemo -> ( -> IsOn BOOLEAN -> );
现在检查上表的内部结构。查询如下-
mysql> show create table BooleanDemo;
输出结果
+-------------+----------------------------------------------------------------------------------------------------------------------------------+ | 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)
看上面的示例输出,BOOLEAN被转换为tinyint(1)。BOOL数据类型也是如此。创建表的查询如下-
mysql> create table BOOLDemo -> ( -> validUser BOOL -> );
现在检查表的内部结构。查询如下-
mysql> show create table BOOLDemo;
输出结果
+----------+------------------------------------------------------------------------------------------------------------------------------------+ | 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)