/    /  MySQL – Add Column

MySQL – Add Column

 

MySQL - Add Column

The ALTER TABLE ADD COLUMN command in MySQL allows the addition of a new column to an existing table.

Syntax:

ALTER TABLE table_name   
    ADD COLUMN column_name column_definition [FIRST|AFTER existing_column];  
  • The first step is to specify the name of the table.
  • The next step is to specify the name and definition of the newly added column after the ADD COLUMN clause.
  • The final step is to specify the FIRST or AFTER keyword. A column can be added to a table as the first column by using the FIRST keyword. A new column is added after an existing column using the AFTER keyword. When these keywords are not provided, MySQL automatically inserts the newly created column as the last column in the table.

Example

ALTER TABLE employees
  ADD age INT NOT NULL
    AFTER last_name;

MySQL - Add Column

In some cases, it may be necessary to add multiple columns to an existing table. Then we can use the following syntax:

Syntax:

ALTER TABLE table_name   
    ADD COLUMN column_name1 column_definition [FIRST|AFTER existing_column],  
    ADD COLUMN column_name2 column_definition [FIRST|AFTER existing_column];  

Example:

 ALTER TABLE employee
  ADD city varchar(40) NOT NULL
    AFTER last_name,
  ADD country varchar(35) NULL
    AFTER city;