对于值为 1 和 0 的字段,我应该使用 MySQL 枚举还是 tinyint?

mysqlmysqli database更新于 2023/11/5 12:52:00

对于值为 0 和 1 的字段,请使用 TINYINT。让我们首先创建一个表 −

mysql> create table DemoTable
   -> (
   -> IsMarried tinyint
   -> );
Query OK, 0 rows affected (0.94 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable values(1);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(0);
Query OK, 1 row affected (0.13 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable;

这将产生以下输出 −

+-----------+
| IsMarried |
+-----------+
|         1 |
|         0 |
+-----------+
2 rows in set (0.00 sec)

相关文章