How to Alter Table in MySQL or MariaDB by using Alter Statement | MySQL Tutorial for developers

How to Alter Table in MySQL or MariaDB by using Alter Statement


Alter table can be used to make changes to existing tables. Here are some of the scenarios we are going to cover.

How to add column to existing table : 

Below syntax can be used to add column to table.


Alter table TableName
Add ColumnName datatype;


Example :

Let's say if we want to add Address column to customer table with data type varchar(100).


Alter table customer
add Address VARCHAR(150);



Modify Data type of Column in Table :

Let's say if you want to change the data type of a column firstname from varchar (30) to varchar (50).


Alter table customer
Modify FirstName VARCHAR(50);



Drop Column from a Table in MySQL:

Let's say if you want to drop the column age from customer, you can use below statement.


Alter table customer
drop column Age;



How to Add Column, Drop Column or Change Data type of a Column by using Alter Table in MySQL

1 comment: