Introduction
In this tutorial, I am going to explain about altering Table Commands in MySQL with examples.
Modifying
Themodifycommand is used when we have to modify a column in the existing table, like adding a new one, modifying the datatype for a column, and dropping an existing column. By using this command, we have to apply some changes to the result set field. This command allows more or fewer characters than before.
To understand the concept of MySQL ALTER TABLE Commands, let’s take a sample database and a table. So, without wasting time, let’s start.
CREATE DATABASE CsharpCorner;
USE CsharpCorner;
CREATE TABLE CsharpCorner_MVPs (
MVPID INT AUTO_INCREMENT PRIMARY KEY,
MVPName VARCHAR(100) NOT NULL,
MVPForYear INT NOT NULL,
MVPAddress VARCHAR(100)
);
1) Add a new column to the existing table
ALTER TABLE and ADD COLUMN statement is used to add one or more new columns to an existing table.
a) Add a single column to a table.
Syntax
ALTER TABLE <table_name>
ADD COLUMN <new_column_name> <Datatype> [CONSTRAINTS];
Example.
ALTER TABLE csharpcorner_mvps
ADD COLUMN MVPKitStatus INT;
Note. Use the “DESCRIBE” command to check the result from the ALTER TABLE ADD statement.

b) Add multiples column to a table.
Syntax
ALTER TABLE employees
ADD COLUMN date_of_birth DATE NOT NULL,
ADD COLUMN department VARCHAR(50) DEFAULT 'IT',
ADD COLUMN salary DECIMAL(10,2) NOT NULL;
Example.
ALTER TABLE csharpcorner_mvps
ADD COLUMN Country VARCHAR(50),
ADD COLUMN Description VARCHAR(250);
Note. Use the “DESCRIBE” command to check the result from the ALTER TABLE ADD statement.

ALTER TABLE MODIFY statement is used to modify one or more columns to an existing table.
a) Modify a single column in a table
Syntax
ALTER TABLE employees
MODIFY employee_name VARCHAR(100);
Example.
ALTER TABLE csharpcorner_mvps
MODIFY MVPKitStatus VARCHAR(100);
Note.
Use the “DESCRIBE” command to check the result from the ALTER TABLE MODIFY statement.

b) Modify multiple columns in a table
Syntax






Join the conversation! Your thoughts help the community grow.