Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 285195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:31:28+00:00 2026-05-12T05:31:28+00:00

To add a NOT NULL Column to a table with many records, a DEFAULT

  • 0

To add a NOT NULL Column to a table with many records, a DEFAULT constraint needs to be applied. This constraint causes the entire ALTER TABLE command to take a long time to run if the table is very large. This is because:

Assumptions:

  1. The DEFAULT constraint modifies existing records. This means that the db needs to increase the size of each record, which causes it to shift records on full data-pages to other data-pages and that takes time.
  2. The DEFAULT update executes as an atomic transaction. This means that the transaction log will need to be grown so that a roll-back can be executed if necessary.
  3. The transaction log keeps track of the entire record. Therefore, even though only a single field is modified, the space needed by the log will be based on the size of the entire record multiplied by the # of existing records. This means that adding a column to a table with small records will be faster than adding a column to a table with large records even if the total # of records are the same for both tables.

Possible solutions:

  1. Suck it up and wait for the process to complete. Just make sure to set the timeout period to be very long. The problem with this is that it may take hours or days to do depending on the # of records.
  2. Add the column but allow NULL. Afterward, run an UPDATE query to set the DEFAULT value for existing rows. Do not do UPDATE *. Update batches of records at a time or you’ll end up with the same problem as solution #1. The problem with this approach is that you end up with a column that allows NULL when you know that this is an unnecessary option. I believe that there are some best practice documents out there that says that you should not have columns that allow NULL unless it’s necessary.
  3. Create a new table with the same schema. Add the column to that schema. Transfer the data over from the original table. Drop the original table and rename the new table. I’m not certain how this is any better than #1.

Questions:

  1. Are my assumptions correct?
  2. Are these my only solutions? If so, which one is the best? I f not, what else could I do?
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:31:28+00:00Added an answer on May 12, 2026 at 5:31 am

    I ran into this problem for my work also. And my solution is along #2.

    Here are my steps (I am using SQL Server 2005):

    1) Add the column to the table with a default value:

    ALTER TABLE MyTable ADD MyColumn varchar(40) DEFAULT('')
    

    2) Add a NOT NULL constraint with the NOCHECK option. The NOCHECK does not enforce on existing values:

    ALTER TABLE MyTable WITH NOCHECK
    ADD CONSTRAINT MyColumn_NOTNULL CHECK (MyColumn IS NOT NULL)
    

    3) Update the values incrementally in table:

    GO
    UPDATE TOP(3000) MyTable SET MyColumn = '' WHERE MyColumn IS NULL
    GO 1000
    
    • The update statement will only update maximum 3000 records. This allow to save a chunk of data at the time. I have to use “MyColumn IS NULL” because my table does not have a sequence primary key.

    • GO 1000 will execute the previous statement 1000 times. This will update 3 million records, if you need more just increase this number. It will continue to execute until SQL Server returns 0 records for the UPDATE statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whats wrong with my query? ALTER TABLE test_posts ADD sticky boolean NOT NULL default
I need to add a NOT NULL column to an existing (populated) table that
I have to add a column whose default value is not null by default
I am trying to add NOT NULL and a DEFAULT to an existing table.
can you tell me how this line works.... my OperatorFactory.get(add) is not doing anything.
Before posting this question, I have tried so many things but that was not
For some reason this is not persisting anything into tag_item table : Tag tag
I am trying to create one to many relation using one table. Is this
I've seen many times the following syntax which defines a column in a create/alter
I am wondering why when I try to not add my object in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.