Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a boolean datatype column to an existing table in sql?

Tags:

I have a table called person in my database. I want to add another column to the same table and it's a Boolean datatype column. I have tried following queries but it says syntax error near default. I know this is a common and there are lot of answers. I have tried many of them and couldn't figure out to make it work. So please help me.

queries I have tried

ALTER TABLE person add column "AdminApproved" BOOLEAN SET default FALSE; ALTER TABLE person alter column "AdminApproved" BOOLEAN SET default FALSE;          

I have tried without SET key word too.

like image 559
Mike Avatar asked Oct 25 '16 15:10

Mike


People also ask

How do I add a boolean column to a table 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.

How do I add a boolean datatype in SQL?

You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.

How do you add a boolean value to a table?

You will get the following output where the true and false literal gets converted into 0 and 1 value. Since MySQL always use TINYINT as Boolean, we can also insert any integer values into the Boolean column. Execute the following statement: Mysql> INSERT INTO student(name, pass) VALUES('Miller',2);

How do I add a column to an existing table in SQL query?

Syntax. The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows.


2 Answers

In SQL SERVER it is BIT, though it allows NULL to be stored

ALTER TABLE person add  [AdminApproved] BIT default 'FALSE'; 

Also there are other mistakes in your query

  1. When you alter a table to add column no need to mention column keyword in alter statement

  2. For adding default constraint no need to use SET keyword

  3. Default value for a BIT column can be ('TRUE' or '1') / ('FALSE' or 0). TRUE or FALSE needs to mentioned as string not as Identifier

like image 59
Pரதீப் Avatar answered Oct 18 '22 08:10

Pரதீப்


The answer given by Pரதீப் creates a nullable bool, not a bool, which may be fine for you. For example in C# it would create: bool? AdminApprovednot bool AdminApproved.

If you need to create a bool (defaulting to false):

    ALTER TABLE person     ADD AdminApproved BIT     DEFAULT 0 NOT NULL; 
like image 22
MEC Avatar answered Oct 18 '22 09:10

MEC