MySQL

How to Add a Column in a Table in MySQL

In this tutorial, we are going to see how to add a column in a table in MySQL. The ALTER TABLE statement is used to add, remove or modify columns in an existing table. Here is the syntax for adding a new column to a MySQL table:

ALTER TABLE tableName
    ADD newColumn type
      [ FIRST | AFTER columnName ];

 

 

Example 1: Add a single column in a Table in MySQL
ALTER TABLE users
  ADD age int NOT NULL
    AFTER name;

This query will add a column called “age” to the “users” table, and will appear after the “name” column.
 

Example 2: Add multiple columns in a Table in MySQL
ALTER TABLE users
  ADD age int NOT NULL
    AFTER name,
  ADD address varchar(50) NOT NULL
    AFTER age;

This query will add two columns to the “users” table: age and address.
 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *