SQL alter table add column

Contents

 

In this tutorial lesson I will show you how to use SQL add column command that will add new column to existing table. Our example will be presented using MS SQL Server 2008 R2.
 

SQL Add Column overview

Before we go into the details let me just say that adding a column is relatively easy; we just need to start with ALTER TABLE and specify the table name (if you use more than one schema in your database you will also have to specify it) after that we'll just need to use SQL Server ALTER TABLE Add column command and specify column properties. Below I will describe column properties you can set:


Column Name - This is obviously mandatory.
Column Date Type - This property is also mandatory and describes the type of values you want to store in the new field.
"Allow NULLs" - This property tells us if the field is mandatory or not and Accepts two values NULL or NOT NULL. If you omit this property a default setting will be used which NULL and means the field is not mandatory (optional).
There are several other popular column properties that you can add to your statement like defaul value, constraint, enable/disable trigger.
 

SQL Add column examples

In our example we will use the table below (‘Person’) in database ‘tutorial’ and using SQL Server ALTER TABLE Add column ‘PersonCity’.

SQL Add Column

USE tutorial




The end result is new column with values NULL in it (see below)
SQL Alter table add column

Next we will add another column ‘DOB’ which in our example will be mandatory so in SQL Server we use NOT NULL. See example below.

USE tutorial



This would work on an empty table but in our case it will fail because we have data in our table and we will get error from SQL Server:
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'DOB' cannot be added to non-empty table 'Person' because it does not satisfy these conditions.

Solution? We need to specify default value that will be inserted for existing rows and in our case we will insert 1900-01-01.
USE tutorial




Here is the end result.

Add column with default value
This approach worked and we inserted new column that will be mandatory; the problem we have is if DOB is not specified then default value (1900-01-01) may be used; which in this case is not what we want.
In order to solve this ‘data quality’ problem we should remove default value straight away but additionally someone should update existing rows... (Which probably won’t happen... But let’s leave this subject alone).

See below how to remove default value from DOB column in order to get truly mandatory field. We do that by simply not including default value:

USE tutorial



Notice that I still included all other information about the column if I didn’t do that they would change to default values; for instance when adding new column by default it is not mandatory.

We finish this tutorial with an example how to add multiple examples at the same time. We do that by simply putting comma (,) at the end of each new column and put next column after it (without add).

USE tutorial



See below final result of our table.Add column with default value
Take care
Emil


SHARE:


Name: Josue
Comment: Now, How can you change the position of the new field in the table?
Date Posted: 15/11/2011 8:09:41 PM

Name: Emil
Comment: Hi Josue, MSDN (microsoft) does not mention anything in their documentation for alter table add column that would indicate that you could do that. I am aware this is supported in other databases but not in SQL Server as far as I know. There is a way to query table info which shows ordinal position but you cannot update that so the only way I can think of which I would not recommend for many reasons (unless it is very important for you) is to rename existing table, create another table (with proper name) with proper column order and insert existing data. Regards Emil
Date Posted: 16/11/2011 8:43:33 PM

Name: jude
Comment: hello I ave been tryin to add column into my table and change some data type ..but am gettin this error...says "saving change is not permmited..change you have made require the following table to be dropped and re created.
Date Posted: 25/01/2012 1:50:54 PM

Name: Emil
Comment: Hi, This is popular. Send me email and I will reply to you. I will post something about soon.
Date Posted: 30/01/2012 10:24:34 PM

Name: Vince
Comment:

THANK YOU SO MUCH!!!!!!!!!!! I was wondering why it kept putting in a stupid NULL value, and this worked, thanks again!!!!!!


Date Posted: 30/01/2013 6:01:14 PM




Disclaimer: While every caution has been taken to provide our readers with most accurate information and honest analysis, please use your discretion before taking any decisions based on the information in this blog. Author will not compensate you in any way whatsoever if you ever happen to suffer a loss/inconvenience/damage because of/while making use of information in this blog.