Rajendra Gupta
sp_help command

SQL Add Column operations

April 22, 2019 by

This article explains the SQL add column operation into an existing SQL table. We will also explore different examples of SQL add column operations.

Sometimes we want to add columns into an existing table. In existing tables, we might have records in it. We do not want to lose existing data as well. In many circumstances, we can drop the tables and recreate them but this is not recommended generally, especially in a production environment, as it can be destructive as it pertains to data. We can still perform a SQL add column operation using Alter Table command, which avoids have to drop tables, delete data, even if only temporarily.

Syntax

We can perform a SQL add column operation on a table with the following transact SQL command.

Prepare the environment

We need to select a Database table and insert data into it.

Execute the following query to create an Employee table in SQLShackDemo database.

Execute the following query to insert sample data into it.

In the following screenshot, we can see the existing data in the Employee table.

Sample data

SQL add column operation on an existing SQL table

We want to add the column department in the Employee table. Suppose we have many columns in a table; we need to check if a particular column exists in the SQL table or not. If the specified column does not exist, we want to create it with the appropriate data type.

We can use the INFORMATION_SCHEMA view to check tables and their columns within a database. Execute the following code to get a list of columns, their data type in Employee table.

In this output, we can see the Employee table contains 4 columns.

Get data about column

Let’s add a new column Department with following Alter Table command.

Execute this query and select records from the Employee table. In the following screenshot, we can look at the new column Department. All existing records contain a NULL value in this column.

SQL Add Column

Previously, we checked all columns in the Employee table using INFORMATION_SCHEMA view. In the following query, we want to create a Department table only if it does not exist in the Employee table.

We can add a column in an existing table if it allows NULL values or have a default value defined on it. We can try to add Not NULL column in the existing SQL table, but it gives the following error message,

Error message in adding column

SQL add column operation to an existing SQL table with a default value

Suppose we want to add the column IsActive column into the Employee table. We can have the following values in this column

  • Value 1: Employee is active
  • Value 0: Employee is not active

By default, all existing and new employee should have Value 1 in IsActive column. We can specify a value using default constraint.

If we try to add a column with a Not NULL value in the existing SQL table, we get following error message,

Execute this query and Select records from a table. For existing records, it does not update the default values.

SQL Add Column

If we insert any new record in this table, it gets default value as per the following screenshot.

SQL Add Column - Insert new row

SQL add column operation to an existing SQL table with an identity column

In SQL Server, we use the Identity function to define a default and auto increment value for each new row. We can add an identity column to the existing SQL table as well. Let’s create a new table Employee_new without an identity column.

Once the table is there, we can add an identity column with the following query.

We created the Identity column in a table without any record in it. Let’s drop the table and recreate it. Insert a few records with the following query.

We have data in the Employee_new table. Let’s add an Identity column with Alter table command.

In the following screenshot, we can see it updates existing records as well.

Multiple SQL add column operations for an existing SQL table with an identity column

We might need to add multiple columns to an existing SQL table. We can do it within the same Alter table command.

In the following query, we added two columns ZipCode and StateCode in a single Alter Table command. We need to specify all columns to add in a similar format.

We can get details of all columns and their properties using sp_help command.

Output of sp_help table

SQL add column operation to an existing SQL table with the table designer in SSMS

In previous examples, we used t-SQL to add columns in the existing table. We might not be familiar with writing t-SQL code. We can use the SSMS GUI as well to add a column.

Right click on the table and click on Design.

Table designer in SSMS

It opens a table designer. We can see all existing column, their data types, default values and other properties for a specified table in a table designer

 view existing columns

Provide a column name and select data types from the drop-down. We can add multiple columns in this with appropriate data types.

Once done, Save and exit the table designer in SSMS. If you try to close it without saving changes, we get a warning message as well.

Error in SSMS

Click on Yes to save new column in the existing table. We can either run a Select statement to verify the new column or use sp_help command to list all columns and their properties.

In the following screenshot, we can see a new column in the Employee table.

sp_help command

Conclusion

In this article, we explored SQL add column operations to add a a new column to an existing SQL table. We can use both the GUI and transact SQL method to do it. I hope you found this article helpful. You can provide feedback or comments in the comments section below.

Rajendra Gupta
Latest posts by Rajendra Gupta (see all)
168 Views