Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create boolean column in MySQL with false as default value?

Tags:

mysql

People also ask

How do I create a new boolean column in MySQL?

ALTER TABLE users ADD bio VARCHAR(100) NOT NULL; Adding a boolean column with a default value: ALTER TABLE users ADD active BOOLEAN DEFAULT TRUE; MySQL offers extensive documentation on supported datatypes in their documentation.

Is boolean default value False?

The default value of any Object , such as Boolean , is null . The default value for a boolean is false.

What is the default value for Boolean data type in SQL?

If you are making the boolean column as not null then the default 'default' value is false; you don't have to explicitly specify it.

How do you create a column with Boolean datatype in SQL?

ALTER TABLE table_name ALTER COLUMN col_name SET NOT NULL; Or you can put them all together in a single statement: ALTER TABLE table_name ADD COLUMN “col_name” BOOLEAN DEFAULT FALSE; This way it might take longer if the operation is huge.


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)

You can set a default value at creation time like:

CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
Married boolean DEFAULT false);

Use ENUM in MySQL for true / false it gives and accepts the true / false values without any extra code.

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

If you are making the boolean column as not null then the default 'default' value is false; you don't have to explicitly specify it.