Create boolean column in MySQL with false as default value?

I want to create a table in MySQL with a boolean column, the default value is false . But it accepts NULL as default value ...

+81
mysql
Feb 08 '10 at 11:00
source share
2 answers

You must specify 0 (false) or 1 (true) as the default. Here is an example:

 create table mytable ( mybool boolean not null default 0 ); 

FYI: boolean is an alias for tinyint(1) .

Here is the proof:

 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 performed in the following version of MySQL:

 mysql> select version(); +----------------+ | version() | +----------------+ | 5.0.18-max-log | +----------------+ 1 row in set (0.00 sec) 
+143
Feb 08 '10 at 11:02
source share

Use ENUM in MYSQL to get true / false and accept true / false without any additional code.

ALTER TABLE itemcategory ADD aaa ENUM ('false', 'true') NOT NULL DEFAULT 'false'

+7
09 Oct '15 at 3:42
source share



All Articles