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

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


Remove Ads or Log in

We need you!

By becoming a proud member we can:
  • Switch off annoying ads
  • Create more articles and videos
  • Develop more useful features supporting your learning
  • Provide FREE webinars (for members)
  • Feel our hard work since 2011 is appreciated
Become a proud member and get exclusive features.

If 2% of our 64,943 visitors (Last 30 days) become a proud member than we can quit our jobs and work full time on this website. We started membership in May 2014 and so far 0.07% of our visitors are members and we would like to say THANK YOU to those who become a proud member and support our efforts.

Thanks to members:
  • Education is for FREE and we create more FREE articles and videos
  • We provide new features (members) that can speed up learning.
  • 53% of our page views (82,000 each month) do not have page ads.
  • We switched video ads from 14% of our videos (50 out of 345).
We have invested 4 years of our time in this website and we estimate it will take about 5 years before we can quit our jobs and dedicate all our time to develop considerably our website which should help to truly achieve our vision of free education for everyone; all thanks to support of our members only. Become a proud member

Visitor Voting

Do you enjoy your job?
Visitor Voting: Yes +15  |  No -5
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
Comment:

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
  Become a Proud Member