ALTER TABLE WITH A DEFAULT: SQL SERVER VS ORACLE

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
 1039  View(s)
Ratings:
Rate this:
Hi Mindstickians!

We have some tables in an Oracle database with several million rows. When we alter one of these tables to add a new column, we specify a default. This is very slow to 

run as Oracle has to update all existing rows with the default. The solution is to ensure the column is defined as NOT NULL because then Oracle (recent versions only) 

will not update all existing rows with the default - the subsequent presence of a null in one of these columns tells Oracle that it requires a default and it will 

provide the default on the fly.

My question is regarding SQL Server: does it exhibit similar behaviour when adding a column and providing a default? If not, are there any best practices in 

efficiently adding new columns with default values, and are there any advantages in defining a column as NOT NULL?

Thanks in advance!
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: ALTER TABLE with a default: SQL Server vs Oracle

    Hi Goti!


    Prior to SQL Server 2012 adding a NULLable column w/o default was very vast, but adding a DEFAULT contraint would be slow, as every row has to be updated. Since SQL Server 2012 http://rusanu.com/2011/07/13/online-non-null-with-values-column-add-in-sql-server-11/ when possible.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!