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.
Did you find this page helpful? Yes! [+0]
| No? [-0]