Howto delete a column from a table in MySQL/MariaDB Databases
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
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.
If You Appreciate What We Do Here On Mimastech, You Should Consider:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.