How to Drop Column in MySQL via Command Line

Posted on

This is an article where the focus is about executing a specific query in MySQL Database Server. The specific query itself is a query to drop column in MySQL Database Server. In order to drop a specific column in a MySQL Database Server, the following are steps to be able to achieve it via command line :

1. Make sure MySQL Database Server has already installed.

2. Make sure the service of MySQL Database Server is active by checking the status of the service whether it is running or not. To be able to check it, just read the following article titled ‘Check MySQL Service Status’ in this link.

3. Logged in to MySQL Database Server where in the context of this article, it is logged in to the MySQL Command Console. In order to logged in to MySQL Command Console, the following article titled ‘Check MySQL Service Status’ can be read as a reference which is available in this link.

The following is the example of logging in to MySQL Command Console for executing the command of dropping a column in MySQL Database Server :

user@hostname:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 104
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  1. Select the database where the associated column which is going to be drop exist. Below is how to execute it :
mysql> use mydb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>

The last thing is definitely dropping the column from the selectable database done in the previous step. Below is the pattern and also an example given for dropping a column in MySQL Database Server :

alter table table_name drop column_name

Description : 
alter : It is a reserved SQL Query keyword for altering table's structure.
table : It is a reserved SQL Query keyword for pointing the entity which is going to be operated and in the context of this article, it is a table. 
table_name : It is the name of a table which is going to be altered
drop : It is a reserved SQL Query keyword which is used to drop an entity or an item which in the context of this article it is a part of the table, specifically it is a column. 
column_name : It is the name of the column which is going to be dropped. 

For an example :

mysql>alter table user drop id_app;
Query OK, x rows affected (0,03 sec)
Records: x  Duplicates: 0  Warnings: 0 

mysql> 

The above command is dropping a column named id_app which is located in a table named ‘user’.

One thought on “How to Drop Column in MySQL via Command Line

Leave a Reply