35

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.

Mike
  • 1,017
  • 4
  • 19
  • 34
  • 4
    Nothing on Google if you search 'Boolean data type sql server'?? You are correct, this is extremely common and there are A LOT OF ANSWERS OUT THERE. – dfundako Oct 25 '16 at 15:40

4 Answers4

61

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

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
  • 9
    Just a caveat here. BIT is NOT a true boolean. It allows 3 states, 0, 1 and NULL. It is subtle but very important to understand. Also you can't code against it like a boolean. – Sean Lange Oct 25 '16 at 15:43
  • 2
    This creates a nullable bool. This is correct : `ALTER TABLE [person] add [AdminApproved] BIT DEFAULT 0 NOT NULL;` – Nabeel May 05 '22 at 16:52
43

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;
MEC
  • 1,690
  • 1
  • 17
  • 23
1

In phpmyadmin, If you need to add a boolean datatype column to an existing table with default value true:

ALTER TABLE books
   isAvailable boolean default true;
Zafar Faheem
  • 2,014
  • 1
  • 7
  • 7
0

There are two ways that you can do it.

ALTER TABLE `tablename`
ADD `column_name` BOOLEAN DEFAULT FALSE 

ALTER TABLE `tablename`
ADD `column_name` TINYINT DEFAULT 0