Create boolean column in MySQL with false as default value?

后端 未结 4 966
醉梦人生
醉梦人生 2021-01-30 04:32

I want to create a table in MySQL with a boolean column whose default value is false. But it\'s accepting NULL as default...

4条回答
  •  不思量自难忘°
    2021-01-30 05:22

    You have to specify 0 (meaning false) or 1 (meaning 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 done on the following version of MySQL:

    mysql> select version();
    +----------------+
    | version()      |
    +----------------+
    | 5.0.18-max-log |
    +----------------+
    1 row in set (0.00 sec)
    

提交回复
热议问题