Howto delete a column from a table in MySQL/MariaDB Databases

undefined

In this post, I’ll show you how to drop a column from a table in MySQL/MariaDB databases. Sometimes you need to drop a wrongly created column in your table, or sometimes when you deploy a rails application and there is a database migration, if the deploy fail for any reason and you run it again you may face another failure due to the migration happened in the first try, hence you need to drop the created column first before re-run the deploy again. there are many reasons for dropping a column from a database.

To drop a column from a table, you need to know the column name and the table name you want to drop the table from. Connect to your database server and select the database that contains the table you’ll drop the column from.

For example. I want to drop a column called currency from a table called finance, Run the following command:

ALTER TABLE finance DROP COLUMN currency;

Note that: the COLUMN keyword is optional, as MySQL/MariaDB will accept just DROP currency. Also, to drop multiple columns, you have to separate them by commas and include the DROP for each one.

ALTER TABLE finance
DROP COLUMN currency,
DROP COLUMN country;

This allows you to DROP, ADD and ALTER multiple columns on the same table in the one statement.

I hope this article is good enough for you.
See you in other articles.

undefined

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donate
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *