In this article, you will learn how to drop or delete a single column or multiple columns from an existing table in an SQL server. Sometimes, you need to remove a single or multiple unused or obsolete columns from an existing table. To do this, you need to follow the syntax as given below:
ALTER TABLE table_name
DROP COLUMN column_name;
Where,
Note: If the column that you want to delete has a CHECK constraint, you must delete the constraint first before deleting the column. SQL Server also does not allow you to delete a column that has a PRIMARY KEY or a FOREIGN KEY constraint. So delete these constraints first before removing columns.
If you want to delete more than one column at once, here is the syntax for deleting multiple columns.
ALTER TABLE table_name
DROP COLUMN column_name_1, column_name_2,...;
If the column contains constraints or other dependencies. Then, an error message will be returned. Resolve the error by deleting the referenced constraints.
This SQL statement creates a new table named Employee in the Database:
CREATE TABLE Employee(
id INT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth DATE NOT NULL,
phone VARCHAR(25),
email VARCHAR(255),
city VARCHAR(255)
);
The following statement drops the city
column from the Employee table:
ALTER TABLE Employee
DROP COLUMN city;
The following statement drops the date_of_birth, phone, email
column from the Employee table:
ALTER TABLE Employee
DROP COLUMN date_of_birth, phone, email;
I hope this article will help you to understand how to drop or delete a single column or multiple columns from an existing table in an SQL server.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments