MySQL ALTER TABLE Statement

MySQL Tutorial

MySQL ALTER TABLE Statement

The ALTER TABLE statement in MySQL is used to modify the structure of an existing table without deleting its data.

  • It is one of the most important SQL commands for real projects, exams, and interviews.

 What is ALTER TABLE?

ALTER TABLE allows you to:

  • Add new columns

  • Modify existing columns

  • Drop columns

  • Add / remove constraints

  • Rename columns or tables

 Data inside the table remains intact (unless explicitly dropped).


 Basic Syntax


 ADD Columns

The ADD keyword is used with ALTER TABLE to add new columns to an existing table.
You can add one column or multiple columns in a single statement.
New columns can have data types, default values, and constraints.

Syntax (Single Column)


 

Example


 


ADD Column with DEFAULT Value

A default value is automatically assigned if no value is provided during insert.


 


ADD Column with NOT NULL

The NOT NULL constraint ensures the column cannot store NULL values.


 


ADD Column at Specific Position

MySQL allows placing a column at a specific position using FIRST or AFTER.


 


ADD Multiple Columns

Multiple columns can be added in one statement.


 MODIFY Column (Change Datatype / Constraint)

Change Datatype

Add NOT NULL


RENAME Column (MySQL 8+)


DROP Column

  •  Column and its data are permanently removed

ADD Constraints Using ALTER TABLE

 Add-PRIMARY KEY

 Add-UNIQUE

 Add-FOREIGN KEY

 Add-CHECK


 DROP Constraints

 Drop-PRIMARY KEY

 Drop-FOREIGN KEY

 Drop-UNIQUE


 RENAME Table


ALTER TABLE vs UPDATE (Interview)

FeatureALTER TABLEUPDATE
AffectsTable structureTable data
Changes columnsYes No
Changes rowsNoYes
Common useSchema changeData change

 Common Errors & Warnings

  •  Dropping columns accidentally
  •  Changing datatype causing data loss
  •  Adding NOT NULL when NULL data exists
  •  Forgetting constraint names

Interview Questions & MCQs (Very Important)

Q1. What is ALTER TABLE used for?

A) Insert data
B) Delete records
C) Modify table structure
D) Select data

Answer: C


Q2. Which command adds a column?

ALTER TABLE t ADD column datatype;

A) Correct
B) Incorrect

Answer: A


Q3. Which command removes a column?

A) DELETE COLUMN
B) DROP COLUMN
C) REMOVE COLUMN
D) CLEAR COLUMN

Answer: B


Q4. Can ALTER TABLE delete data?

A) Yes
B) No

Answer: No
(Except dropped columns)


Q5. Which command renames a table?

ALTER TABLE old_name RENAME TO new_name;

A) Correct
B) Incorrect

Answer: A


Q6. Which command adds a FOREIGN KEY?

A) ADD KEY
B) ADD CONSTRAINT
C) ADD INDEX
D) ADD CHECK

Answer: B


 Real-Life Use Cases

  •  Adding new fields to applications
  •  Changing data types
  • Enforcing new constraints
  •  Database version upgrades
  •  Schema maintenance

 Summary

  • ALTER TABLE modifies existing table structure

  • Used to ADD, MODIFY, DROP, RENAME columns

  • Can manage constraints

  • Does not affect existing data (mostly)

  • Very important for SQL exams & interviews

You may also like...