How to add or remove a column from a table in SQL Server?
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
-
CreatedSep 19, 2013
-
UpdatedOct 03, 2020
-
Views4,022