Last Updated: 25 July, 2023
To drop a column from a table in MySQL, we can use the ALTER TABLE statement with the DROP COLUMN clause.
The syntax and example are as follows:
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Replace table_name with the name of the table from which we want to remove the column, and column_name with the name of the column we wish to drop.
Example:
Suppose we have a table called students with the following structure:
Table: students
+----+-------+-------+---------+------+------+--------+ | id | name | class | section | roll | age | gender | +----+-------+-------+---------+------+------+--------+ | 1 | Ram | UKG | B | 18 | 6 | M | | 2 | Pooja | PLAY | A | 38 | 3 | M | | 3 | Raju | UKG | C | 7 | 6 | M | | 4 | Gopi | LKG | A | 4 | 6 | M | +----+-------+-------+---------+------+------+--------+ 5 rows in set (0.00 sec)
If we want to delete the column section from the students table, we will write the following query.
ALTER TABLE students DROP COLUMN section;
After droping the column.
+----+-------+-------+---------+------------+ | id | name | class | roll | age | gender | +----+-------+-------+---------+------------+ | 1 | Ram | UKG | 18 | 6 | M | | 2 | Pooja | PLAY | 38 | 3 | M | | 3 | Raju | UKG | 7 | 6 | M | | 4 | Gopi | LKG | 4 | 6 | M | +----+-------+-------+---------+------+-----+ 5 rows in set (0.00 sec)
Please note that dropping a column will permanently remove the column and its data from the table. Make sure you have a backup or are certain about this action before executing the command. Additionally, if the column has any indexes, constraints, or dependencies, they will also be dropped along with the column.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com