SQL alter table add column

My Learning: Log in Learning Report [Log in]

Remove Ads or Log in
Remove Ads or Log in
+0  |  -0
Author(s): by Katie Glownia & by Emil Glownia



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

Remove Ads or Log in

We need you!

Time of free websites that are supported by ads only is over, due to increasing use of ad blocking.

Our website would die probably within a year; as there is no point investing free time and later on own money in something that is doomed.

Our only hope to survive (without any ads) is to ask our visitors like yourself to support it by becoming a member.

We know it's only going to be a tiny % of visitors who will do that but we hope that will be enough to keep our website for free and within a year switch off all ads. (54% page ads are switched off already for visitors from low income countries and 15% video ads are switched off for everyone).

Become a proud member

Visitor Voting

We are thinking about re-introducing webinars (SQL, SSIS, SSRS, SSAS) but this time for members only (due to effort).
Is that something you might be interested in?
Visitor Voting: Yes +32  |  No -7
Add comment (no email required).

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

Name: Emil
Date Posted: 11/16/2011 8:43:33 PM
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

Name: jude
Date Posted: 1/25/2012 1:50:54 PM
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.

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

Name: Vince
Date Posted: 1/30/2013 6:01:14 PM

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

Name: Murugan
Date Posted: 6/20/2014 3:18:46 AM
Comment: Thank you very much for your post. very easy to understand...

Are you a member?

If yes than please log in

Learning Map is for members with Bronze, Silver and Gold Membership.
For more information visit membership options.

Close this window , log in or learn more about membership benefits