• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / MySQL: SQL to drop a column from a table

MySQL: SQL to drop a column from a table

It’s probably more common for people to edit MySQL tables using a more visual tool such as phpMyAdmin but it is possible to use a manually written SQL query instead to modify tables (this is what a tool like phpMyAdmin does in the background). This post shows how to drop a column from a MySQL table with a SQL query.

Drop a single column

The query to drop a single column is very simple. In the example table "test" there is a field called "foo" that is to be dropped. This is done like so:

ALTER TABLE test
DROP COLUMN foo;

Depending how large the table is this may take a few milliseconds to a number of minutes to run.

Drop multiple columns

Multiple columns can be dropped with a single query by simply comma separating a list of DROP COLUMN statments. To drop both the "foo" and "bar" columns from the "test" table do this:

ALTER TABLE test
DROP COLUMN foo,
DROP COLUMN bar;

As many additional columns can be added to the list as required. Again, this may only take a few milliseconds to many minutes depending on the size of the data in the table.

Check Out These Related posts:

  1. MySQL utility commands
  2. Backing up MySQL with mysqldump
  3. Type casting with PHP
  4. Drop multiple MySQL tables

Filed Under: MySql

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap