SQL Server: How to add a column to table and How to delete a column from table

 SQL Server     sbo     Updated on Sep 19, 2013     VIKRAM     646   

Introduction:

In this article i will explain how to add a column to table and how to delete a column from table in SQL Server.

Description:

In previous articles i explained How to create a table in SQL Server and How to alter table column in SQL Server. Now i will explain how to add a column to table and how to delete a column from table in SQL Server.

First i'm creating Employee table here .

CREATE TABLE Employee(
    EmpNo int,
    EmpName varchar(500),
    EmpSalary decimal(18, 0)
)

insert sample data into Employee table as shown below.

insert into Employee values(1, 'Vikram Reddy', '71000')
insert into Employee values(2, 'Sagar', '38000')
insert into Employee values(4, 'Kiran', '31000')

Now select all employees list from Employee table as shown below.

select * from Employee

Output:

Now i'm adding HireDate column to Employee table.

Add a column to table:

Syntax:

Alter table <table-name> add <column-name> <data-type>

Example:

Alter table Employee add HireDate Date

By default HireDate column will take NULL values.

select * from Employee

Output:

Note: If you want to add multiple columns then mention <column-name> with <data-type> seperated by cama.

Example:

To add HireDate and CreatedDate columns at a time then change the query as follows:

alter table Employee add HireDate Date, CreatedDate DateTime,...

Delete a column from table:

Now i'm deleting HireDate column from Employee table.

Syntax:

Alter table <table-name> Drop column <column-name>

Example:

Alter table Employee Drop column HireDate

 

  Related Articles
Java Script: Create/Insert rows dynamically to HTML Table and Remove/Delete rows from HTML Table
Java Script: Create HTML Table dynamically
SQL Server: Delete record from parent table when there is Foreign Key relation between parent and child tables
SQL Server: NOT NULL Constraint
SQL Server: Temporary Table
SQL Server: How to rename a column and How to rename a table
ASP.Net: ListView Basics - ItemDeleting
SQL Server: How to create a table
  Recent Articles
Java Script: Create/Insert rows dynamically to HTML Table and Remove/Delete rows from HTML Table
Java Script: Create HTML Table dynamically
SQL Server: Delete record from parent table when there is Foreign Key relation between parent and child tables
SQL Server: NOT NULL Constraint
SQL Server: Temporary Table
SQL Server: How to rename a column and How to rename a table
ASP.Net: ListView Basics - ItemDeleting
SQL Server: How to create a table