Sometimes we have to add new columns to existing tables in MySQL. In this post, we will see how we can add new column/columns to existing MySQL tables.
Syntax of adding new column to MySQL table
Alter Table table_name ADD col_nm datatype;
- To add new column we have to modify the table with Alter command
- Put the ADD keyword
- Mention the column name with its datatype.
Example to add new column in MySQL table
We have dept table.
describe dept;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
We want to add a new column owner to dept table. We will execute the below statement:
Alter table dept ADD owner varchar(100);
Column owner will be added to dept table. Lets check whether a new column has been added or not.
Describe Dept;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| owner | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+