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.
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.
In our example we will use the table below (‘Person’) in database ‘tutorial’ and using SQL Server ALTER TABLE Add column ‘PersonCity’.
The end result is new column with values NULL in it (see below)
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.
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.
Here is the end result.
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:
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).
See below final result of our table.
We need you!
By becoming a proud member we can:
Become a proud member
- 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
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