×

How to delete column in table

Introduction

  • In SQL, sometimes it is required to delete a column of a table.
  • The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column from a table.
  • It is possible to remove single as well as multiple columns from a table.

1. Delete a single column from a table

To delete a single column of the table, use the syntax given below:

ALTER TABLE TableName DROP COLUMN ColumnName;

Here,

  • TableName is the name of table whose column is to be deleted.
  • ColumnName after the DROP COLUMN clause is the name of the column which is to be deleted.

          Example:

Consider a database with name “employee_db” with a table ‘employee’ created into it. In this topic, we will consider this table and database for all the subsequent examples:

mysql> USE employee_db;
Database changed
mysql> DESC employee;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Emp_ID      | int(11)     | NO   | PRI | NULL    |       |
| Emp_Name    | varchar(20) | YES  |     | NULL    |       |
| Emp_Salary  | int(11)     | YES  |     | NULL    |       |
| Emp_Dept    | varchar(20) | YES  |     | NULL    |       |
| Emp_City    | varchar(20) | YES  |     | NULL    |       |
| Emp_PhoneNo | varchar(20) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
6 rows in set (0.07 sec)


mysql> SELECT *FROM employee;
+--------+----------+------------+------------+----------+-------------+
| Emp_ID | Emp_Name | Emp_Salary | Emp_Dept   | Emp_City | Emp_PhoneNo |
+--------+----------+------------+------------+----------+-------------+
|    101 | Ram      |      52000 | R&D        | Pune     | 8798654676  |
|    102 | Shyam    |      38000 | Finance    | Delhi    | 9898765687  |
|    103 | Anmol    |      61000 | Accounting | Mumbai   | 9087864532  |
|    104 | Abhishek |      69000 | Purchasing | Shimla   | 7678987534  |
|    105 | Rohit    |      53000 | HRM        | Ambala   | 8897865643  |
+--------+----------+------------+------------+----------+-------------+
5 rows in set (0.00 sec)
How to delete column in table

Here, we have selected the already created database with ‘USE employee_db’ command. “DESC employee” command describes the structure of ‘employee’ table. Then we have used the SELECT command to display the employee table created into employee_db.

Now, we will write a query to delete a column containing employee city in ‘employee’ table.

mysql> ALTER TABLE employee DROP COLUMN Emp_City;
Query OK, 5 rows affected (0.30 sec)
Records: 5  Duplicates: 0  Warnings: 0
How to delete column in table

ALTER TABLE command is used on employee table with DROP COLUMN clause on Emp_City.

mysql> DESC employee;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Emp_ID      | int(11)     | NO   | PRI | NULL    |       |
| Emp_Name    | varchar(20) | YES  |     | NULL    |       |
| Emp_Salary  | int(11)     | YES  |     | NULL    |       |
| Emp_Dept    | varchar(20) | YES  |     | NULL    |       |
| Emp_PhoneNo | varchar(20) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.02 sec)


mysql> SELECT *FROM employee;
+--------+----------+------------+------------+-------------+
| Emp_ID | Emp_Name | Emp_Salary | Emp_Dept   | Emp_PhoneNo |
+--------+----------+------------+------------+-------------+
|    101 | Ram      |      52000 | R&D        | 8798654676  |
|    102 | Shyam    |      38000 | Finance    | 9898765687  |
|    103 | Anmol    |      61000 | Accounting | 9087864532  |
|    104 | Abhishek |      69000 | Purchasing | 7678987534  |
|    105 | Rohit    |      53000 | HRM        | 8897865643  |
+--------+----------+------------+------------+-------------+
5 rows in set (0.00 sec)
How to delete column in table

When we again apply the DESC command on ‘employee’ table just after applying the ALTER command written above, now we can see that Emp_City is not listed in the results. This shows that column named as Emp_City is now deleted from the employee table.

We have also used the SELECT command again. In the results of SELECT command, from all the records the values contained in Emp_City are removed.

2. Delete multiple columns from a table

To delete more than one column of the table, use the syntax given below:

ALTER TABLE TableName DROP COLUMN ColumnName1, DROP COLUMN ColumnName2,……ColumnNameN;

Here,

We need to specify all the column names which are to be removed, with DROP COLUMN clause.

Example:

Firstly, we will see the structure of the employee table and the records present in it.

mysql> DESC employee;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Emp_ID      | int(11)     | NO   | PRI | NULL    |       |
| Emp_Name    | varchar(20) | YES  |     | NULL    |       |
| Emp_Salary  | int(11)     | YES  |     | NULL    |       |
| Emp_Dept    | varchar(20) | YES  |     | NULL    |       |
| Emp_PhoneNo | varchar(20) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.02 sec)


mysql> SELECT *FROM employee;
+--------+----------+------------+------------+-------------+
| Emp_ID | Emp_Name | Emp_Salary | Emp_Dept   | Emp_PhoneNo |
+--------+----------+------------+------------+-------------+
|    101 | Ram      |      52000 | R&D        | 8798654676  |
|    102 | Shyam    |      38000 | Finance    | 9898765687  |
|    103 | Anmol    |      61000 | Accounting | 9087864532  |
|    104 | Abhishek |      69000 | Purchasing | 7678987534  |
|    105 | Rohit    |      53000 | HRM        | 8897865643  |
+--------+----------+------------+------------+-------------+
5 rows in set (0.00 sec)
How to delete column in table

Here, we have selected the already created database with ‘USE employee_db’ command. “DESC employee” command describes the structure of ‘employee’ table. Then we have used the SELECT command to display the employee table created into employee_db.

Now, we will write a query to delete a column containing employee salary and employee phone number in ‘employee’ table.

mysql> ALTER TABLE employee DROP COLUMN Emp_Salary, DROP COLUMN Emp_PhoneNo;
Query OK, 5 rows affected (0.29 sec)
Records: 5  Duplicates: 0  Warnings: 0
How to delete column in table

ALTER TABLE command is used on employee table with DROP COLUMN clause on Emp_Salary and Emp_PhoneNo.

mysql> DESC employee;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| Emp_ID   | int(11)     | NO   | PRI | NULL    |       |
| Emp_Name | varchar(20) | YES  |     | NULL    |       |
| Emp_Dept | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> SELECT *FROM employee;
+--------+----------+------------+
| Emp_ID | Emp_Name | Emp_Dept   |
+--------+----------+------------+
|    101 | Ram      | R&D        |
|    102 | Shyam    | Finance    |
|    103 | Anmol    | Accounting |
|    104 | Abhishek | Purchasing |
|    105 | Rohit    | HRM        |
+--------+----------+------------+
5 rows in set (0.00 sec)
How to delete column in table

When we again apply the DESC command on ‘employee’ table just after applying the ALTER command written above, we can see that Emp_Salary and Emp_PhoneNo is not listed in the results. This shows that column named as Emp_Salary and Emp_PhoneNo is now deleted from the employee table. Then we have also used the SELECT command again. In the results of SELECT command, from all the records the values contained in Emp_Salary and Emp_PhoneNo are removed.


Related Topics

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.