How to: MySQL Delete Column

How do I delete a column from an existing MySQL table using UNIX / Windows / Linux mysql command line utility sql syntax?

You need to use the ALTER TABLE syntax to change the structure of an existing table. For example, you can add or delete columns, create or destroy indexes, change the type of existing columns, and much more with ALTER TABLE.

ADVERTISEMENTS

Step # 1: Login to mysql

Type the following command:
mysql -u user -p databasename

Step 2: Delete column

First see table description:
desc tableName
Use the following syntax at mysql>
ALTER TABLE tableName DROP columnName;
For example, see t1 table description, enter:

DESC t1;

Sample outputs:

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| c1    | int(11)     | YES  |     | NULL    |       | 
| c2    | varchar(30) | YES  |     | NULL    |       | 
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Delete column c2, enter:

ALTER TABLE t1 DROP c2;
DESC t1

Sample outputs:

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| c1    | int(11) | YES  |     | NULL    |       | 
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • Azeem May 4, 2012 @ 12:21

    I want to delete column data only not row for that i have write that query but it doesn’t work so any idea please share me

    DELETE title FROM thinks_books WHERE title=’urdu’,
    Thanks
    Azeem Akram

    • lijubahulayan Sep 5, 2012 @ 10:31

      Dear Azeem, try to execute “UPDATE ” query instead of DELETE

      UPDATE thinks_books SET title=” ” WHERE title=’Urudu’;

  • leela May 13, 2012 @ 16:34

    I created a table in mysql containing 2 columns ,userid,movieid.I entered the values in movieid column using arraylist.

    the table contains the following data

    userid movieid

    1 12234,5688,8976
    2 567

    now i want to delete 12234 in movieid column where userid=1
    how i can write the query in mysql.can anyone help me.

  • mathews Mar 12, 2013 @ 12:06

    how to show agin table

  • Soumyajeet Jul 5, 2016 @ 6:59

    I need to drop two/more columns using only one ‘drop’/’delete’ statement in mySQL.
    It is possible in oracle SQL using ‘drop’.

  • Soumyajeet Jul 5, 2016 @ 7:01

    “ALTER TABLE Table_name DROP column_name1, DROP column_name2;”
    I want to simplify this using only one drop. Is it possible?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.