Bootstrap FreeKB - mySQL / MariaDB - Remove a column using the alter table command
mySQL / MariaDB - Remove a column using the alter table command

Updated:   |  mySQL / MariaDB articles

The alter table command can be used to remove a column from a table. You will first need to log into your MariaDB or mySQL server or configure passwordless authentication and then use the -e command line option and the user must have the Alter_priv.

You will probably want to show the columns in the table that you want to remove a column from.The show columns or describe <tablename> commands can be used to list the columns in the table. In this example, the columns in table001 in the database named db001 will be described.

use db001; describe table001;

 

Or like this.

describe db001.table001;

 

Something like this should be returned.

+-----------------+---------------+------+-----+---------+----------------+
| Field           | Type          | Null | Key | Default | Extra          |
+-----------------+---------------+------+-----+---------+----------------+
| id              | int(11)       | NO   | PRI | NULL    | auto_increment |
| column001       | varchar(200)  | NO   |     | NULL    |                |
| column002       | varchar(1000) | NO   |     | NULL    |                |
| column003       | varchar(2000) | NO   |     | NULL    |                |
+-----------------+---------------+------+-----+---------+----------------+

 

Remove a column.

AVOID TROUBLE

Notice in this example that the "id" column has the PRI (PRIMARY KEY) set and auto_increment. Each table can have only one column set as the PRIMARY KEY. For this reason, be very careful if you are removing the column that has the primary key or auto_increment set. Refer to Understanding PRIMARY KEY and auto_increment.

alter table db001.table001 drop column column003;

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 443ff7 in the box below so that we can be sure you are a human.