You have to specify 0 (meaning false) or 1 (meaning true) as the default. Here is an example:
);
FYI: boolean is an alias for tinyint(1).
mysql> create table mytable (
-> mybool boolean not null default 0
-> );
Query OK, 0 rows affected (0.35 sec)
mysql> insert into mytable () values ();
Query OK, 1 row affected (0.00 sec)
mysql> select * from mytable;
+--------+
| mybool |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
FYI: My test was done on the following version of MySQL:
mysql> select version();
+----------------+
| version() |
+----------------+
| 5.0.18-max-log |
+----------------+
create table mytable (
mybool boolean not null default 0);
FYI: boolean is an alias for tinyint(1).
mysql> create table mytable (
-> mybool boolean not null default 0
-> );
Query OK, 0 rows affected (0.35 sec)
mysql> insert into mytable () values ();
Query OK, 1 row affected (0.00 sec)
mysql> select * from mytable;
+--------+
| mybool |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
FYI: My test was done on the following version of MySQL:
mysql> select version();
+----------------+
| version() |
+----------------+
| 5.0.18-max-log |
+----------------+
1 row in set (0.00 sec)
在mysql中设置字段类型为boolean。mysql会自动变成tinyint(1)。明白了为什么default value 为0/1。
本文介绍了在MySQL中如何使用布尔类型,并解释了其默认值为0或1的原因。布尔类型实际上会被MySQL转换为tinyint(1),并展示了创建包含布尔字段的表及插入数据的示例。
4237

被折叠的 条评论
为什么被折叠?



