How can I drop an existing column from a MySQL table?

MySQLMySQLi Database

We can delete a particular existing column from a MySQL table by using the DROP statement along with an ALTER statement. Its syntax would be as follows −

Syntax

ALTER TABLE table_name DROP column_name;

Here, table_name is the name of the table from which we want to delete the column.

Column_name is the name of the column which is to be deleted from the table.

Example

In this example, we are deleting the column ‘address’ from table ‘student_detail’ as follows −

mysql> select * from student_detail;
+-----------+-------------+----------+
| Studentid | StudentName | address  |
+-----------+-------------+----------+
|       100 | Gaurav      | Delhi    |
|       101 | Raman       | Shimla   |
|       103 | Rahul       | Jaipur   |
|       104 | Ram         | Ludhiana |
|       105 | Mohan       | Patiala  |
+-----------+-------------+----------+
5 rows in set (0.19 sec)

mysql> ALTER TABLE student_detail DROP address;
Query OK, 0 rows affected (1.43 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from student_detail;
+-----------+-------------+
| Studentid | StudentName |
+-----------+-------------+
|       100 | Gaurav      |
|       101 | Raman       |
|       103 | Rahul       |
|       104 | Ram         |
|       105 | Mohan       |
+-----------+-------------+
5 rows in set (0.00 sec)

The above result set shows that column ‘address’ has been deleted from the table.

raja
Published on 22-Feb-2018 13:03:20
Advertisements