How to Add a Column to a MySQL Table

Adding a Column in an Existing MySQL Table

Businesswoman in an office working on a laptop computer
Diane Diederich/E+/Getty Images

The command add column is used to add an additional column to any given MySQL table.

To do this, you must specify the column name and type.

Note: The add column command is sometimes referred to as additional column or new column.

How to Add a MySQL Column

Adding a column to an existing table is done with this syntax:

alter table

add column [new column name] [type];

Here's an example:

alter table icecream add column flavor varchar (20) ; 

What this example would end up doing is adding the column "flavor" to the table "icecream," just as it says above. It would be in the database "varchar (20)" format.

Know, however, that the "column" clause is not required. So, you could instead use "add [new column name]...", like this:

alter table icecream add flavor varchar (20) ; 

Adding a Column After an Existing Column

Something you may prefer to do is add a column after a specified existing column. So, if you'd like to add the column flavor after one called size, you could do something like this:

alter table icecream add column flavor varchar (20) after size; 

Changing a Column Name on a MySQL Table

You can change a column's name with the alter table and change commands. Read more about that in the How to Change a Column Name in MySQL tutorial.

Format
mla apa chicago
Your Citation
Bradley, Angela. "How to Add a Column to a MySQL Table." ThoughtCo, Aug. 26, 2020, thoughtco.com/add-column-mysql-command-2693988. Bradley, Angela. (2020, August 26). How to Add a Column to a MySQL Table. Retrieved from https://www.thoughtco.com/add-column-mysql-command-2693988 Bradley, Angela. "How to Add a Column to a MySQL Table." ThoughtCo. https://www.thoughtco.com/add-column-mysql-command-2693988 (accessed April 24, 2024).