Skip to main content
World Nomads Logo

How to Add multiple column using Alter table in MySQl?

MySQL ALTER Table

MySQL ALTER statement is used when you want to change the name of your table or any table
field. It is also used to add or delete an existing column in a table.

The ALTER statement is always used with "ADD", "DROP" and "MODIFY" commands according to
the situation.

1) ADD a column in the table

Syntax:

ALTER TABLE table_name  
ADD new_column_name column_definition  
[ FIRST | AFTER column_name ];  
Parameters

table_name: It specifies the name of the table that you want to modify.

new_column_name: It specifies the name of the new column that you want to add to the
table.

column_definition: It specifies the data type and definition of the column (NULL or NOT
NULL, etc).

FIRST | AFTER column_name: It is optional. It tells MySQL where in the table to create the
column. If this parameter is not specified, the new column will be added to the end of the
table.

Example:

In this example, we add a new column "cus_age" in the existing table "cus_tbl".

Use the following query to do this:

ALTER TABLE cus_tbl  
ADD cus_age varchar(40) NOT NULL;  
See the recently added column:

SELECT* FROM cus_tbl;  
2) Add multiple columns in the table

Syntax:

ALTER TABLE table_name  
ADD new_column_name column_definition  
[ FIRST | AFTER column_name ],  
ADD new_column_name column_definition  
[ FIRST | AFTER column_name ],  
 ...  
;  
Example:

In this example, we add two new columns "cus_address", and cus_salary in the existing
table "cus_tbl". cus_address is added after cus_surname column and cus_salary is added
after cus_age column.

Use the following query to do this:

ALTER TABLE cus_tbl  
ADD cus_address varchar(100) NOT NULL  
AFTER cus_surname,  
ADD cus_salary int(100) NOT NULL  
AFTER cus_age ;  
See the recently added columns:

SELECT* FROM cus_tbl;  


Continue...

Subscribe channel on YouTube
Read this post on Blogger
Like page on Facebook
Tweet about video on twitter
Let's chat on whatsapp
See you at Instagram
Google+

Comments

Contact Us

Name

Email *

Message *