38

I have a table that I want to add a bit column, which I wish to default to false for all existing data.

How do I alter my table in such a way that it allows me to specify NOT NULL before I have inserted false for my existing rows?

Should I create it as nullable, do an insert than switch it non-nullable?

marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Dec 22, 2009 at 16:24
0

6 Answers 6

86

You could add the column and provide the default value to be used for all existing rows.

ALTER TABLE foo 
ADD bar bit 
DEFAULT 0 NOT NULL;
answered Dec 22, 2009 at 16:29

1 Comment

I typically like to NAME my constraint - even default constraints - in case I need to drop them later on. The default "DF__TimeZones__IsUsa__3D5E1FD2" names are a bit hard to remember.....
8

As an alternative to the answers listed there is another syntax available which may suit some people better, for example if you use tools like 'ReadyRoll'.

ALTER TABLE [dbo].[FOO] ADD BAR bit NOT NULL CONSTRAINT [df_Bar] DEFAULT 0

Check out this SO answer to see why naming your constraints (the df_Bar above) is nice.

answered Aug 2, 2016 at 10:15

Comments

4
ALTER TABLE foo ADD bar bit DEFAULT 0 NOT NULL WITH VALUES;

The "with values" clause propigates the default value into existing rows.

answered Dec 22, 2009 at 16:36

4 Comments

@marc_s, I just ran it without the "WITH VALUES" for my bit column and it worked. Are you sure?
@KingNestor, try it with a default value of 1, or something that's not all binary zeros.
WITH VALUES is used in the case of a NULLABLE column and you want the default value to be used instead of NULL.
@KingNestor: you're absolutely right - if the column is defined as NOT NULL, it will be filled with the default value even without the "WITH VALUES" clause. Thanks for pointing that out! But the WITH VALUES is needed if the column is defined as NULLable
1
ALTER TABLE dbo.MyTable ADD MyColumn bit NOT NULL DEFAULT 0

For what it is worth, you can fire up Enterprise Manager, make the changes in the UI, and then have it generate a Change Script - and you can see how it would accomplish these kinds of tasks.

answered Dec 22, 2009 at 16:35

Comments

0

I have also done it as you say "create it as nullable, do an insert than switch it non-nullable". I've not had a problem doing it this way.

I've not yet needed to find a better way, however I'm intrigued if there is another way....

answered Dec 22, 2009 at 16:26

Comments

0

I usually create the field as nullable with a default as false in your case update all the fields that were in the database prior then switch it to null

answered Dec 22, 2009 at 16:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.