ALTER TABLE in SQL | Complete Guide with Examples and Interview Questions

ALTER TABLE in SQL

In database management, requirements often change after a table has already been created. New columns may need to be added, existing columns may require modification, unnecessary columns may need to be removed, or table structures may need adjustments. SQL provides the ALTER TABLE statement to handle such changes without recreating the entire table.

The ALTER TABLE command is one of the most frequently used Data Definition Language (DDL) commands. It allows database administrators and developers to modify the structure of existing tables while preserving the data already stored within them.

Whether you are maintaining a student management system, banking application, hospital database, or e-commerce platform, understanding ALTER TABLE is essential because database structures frequently evolve as applications grow.


What is ALTER TABLE?

The ALTER TABLE statement is used to change the structure of an existing table. It allows developers to add new columns, modify existing columns, rename columns, remove columns, add constraints, remove constraints, and rename tables.

Unlike CREATE TABLE, which creates a new table, ALTER TABLE modifies an already existing table.


Why Use ALTER TABLE?


Basic Syntax of ALTER TABLE

ALTER TABLE TableName
Operation;

The operation can be adding, modifying, renaming, or deleting table components.


Adding a New Column

One of the most common uses of ALTER TABLE is adding a new column to an existing table.

Syntax

ALTER TABLE Student
ADD Email VARCHAR(100);

Example

Suppose the Student table initially contains:

StudentID StudentName

After executing:

ALTER TABLE Student
ADD Email VARCHAR(100);

The table now contains StudentID, StudentName, and Email columns.


Adding Multiple Columns

Multiple columns can be added in a single statement in many database systems.

ALTER TABLE Employee
ADD (
Department VARCHAR(50),
Salary DECIMAL(10,2)
);

This adds both Department and Salary columns to the Employee table.


Modifying an Existing Column

Sometimes a column's data type or size needs to be changed.

Syntax (MySQL)

ALTER TABLE Student
MODIFY StudentName VARCHAR(200);

This increases the maximum length of StudentName from its previous size to 200 characters.

Practical Scenario

Initially, employee names were limited to 50 characters. Later, the organization decides to allow longer names. ALTER TABLE helps make this change without affecting existing records.


Changing a Column Data Type

ALTER TABLE Product
MODIFY Price DECIMAL(12,2);

This increases the precision available for storing product prices.


Renaming a Column

Column names may need updating for clarity or standardization.

Syntax

ALTER TABLE Student
RENAME COLUMN Name TO StudentName;

The column Name becomes StudentName.


Dropping a Column

Unused columns can be removed from a table using ALTER TABLE.

Syntax

ALTER TABLE Student
DROP COLUMN Email;

The Email column and all data stored in it are permanently removed.


Important Warning Before Dropping Columns


Renaming a Table

Sometimes the entire table name must be changed.

Syntax

ALTER TABLE Student
RENAME TO Students;

The Student table is renamed to Students.


Adding Constraints Using ALTER TABLE

Constraints can be added after table creation.

Add Primary Key

ALTER TABLE Student
ADD PRIMARY KEY (StudentID);

Add Unique Constraint

ALTER TABLE Student
ADD CONSTRAINT UQ_Email
UNIQUE (Email);

Add Check Constraint

ALTER TABLE Student
ADD CHECK (Age >= 18);

Adding a Foreign Key

Foreign Keys establish relationships between tables.

ALTER TABLE Employee
ADD CONSTRAINT FK_Department
FOREIGN KEY (DepartmentID)
REFERENCES Department(DepartmentID);

This ensures DepartmentID values match existing records in the Department table.


Dropping Constraints

Constraints may also be removed when requirements change.

Drop Unique Constraint

ALTER TABLE Student
DROP CONSTRAINT UQ_Email;

Drop Primary Key

ALTER TABLE Student
DROP PRIMARY KEY;

The exact syntax may vary slightly between database systems.


Real-World Example: Student Management System

Initially:

CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100)
);

Later requirements demand additional information:

ALTER TABLE Student
ADD Email VARCHAR(100);

ALTER TABLE Student
ADD PhoneNumber VARCHAR(15);

ALTER TABLE Student
ADD AdmissionDate DATE;

The table evolves without recreating it.


Real-World Example: E-Commerce Application

An online store initially stores only product names and prices.

CREATE TABLE Product (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(8,2)
);

Later, inventory tracking is required.

ALTER TABLE Product
ADD StockQuantity INT;

This enhancement supports inventory management.


Common ALTER TABLE Operations Summary

Operation Purpose
ADD Add new columns
MODIFY Change data type or size
DROP COLUMN Remove columns
RENAME COLUMN Rename columns
RENAME TO Rename table
ADD CONSTRAINT Add constraints
DROP CONSTRAINT Remove constraints

Best Practices for ALTER TABLE


Common Mistakes to Avoid


Advantages of ALTER TABLE


Performance Considerations

Large tables containing millions of records may require additional processing time when structural changes are applied.


ALTER TABLE vs CREATE TABLE

Feature CREATE TABLE ALTER TABLE
Purpose Create a new table Modify an existing table
Existing Data No Preserved
Usage Stage Initial design Maintenance and updates
Flexibility Creates structure Changes structure

Interview Questions on ALTER TABLE

1. What is ALTER TABLE in SQL?

ALTER TABLE is a DDL command used to modify the structure of an existing table.

2. Can ALTER TABLE add new columns?

Yes. The ADD keyword is used to insert new columns into a table.

3. How do you remove a column from a table?

ALTER TABLE Student
DROP COLUMN Email;

4. Can ALTER TABLE change a data type?

Yes. The MODIFY or ALTER COLUMN clause is used depending on the database system.

5. What happens when a column is dropped?

The column and all data stored within it are permanently removed.

6. Why should backups be created before ALTER TABLE operations?

Because structural changes may result in accidental data loss or application issues.

7. Can constraints be added using ALTER TABLE?

Yes. Primary Keys, Foreign Keys, Unique Constraints, and Check Constraints can be added.

8. Can a table be renamed using ALTER TABLE?

Yes. The RENAME TO statement is used for this purpose.

9. What is the difference between MODIFY and ADD?

ADD creates new columns, while MODIFY changes existing columns.

10. Is ALTER TABLE a DDL or DML command?

ALTER TABLE is a Data Definition Language (DDL) command.

11. Can ALTER TABLE affect performance?

Yes. Structural modifications on large tables may require significant processing time.

12. What precautions should be taken before altering a production table?

Create backups, test changes, review dependencies, and schedule maintenance appropriately.


Complete Practical Example

CREATE TABLE Employee (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(100)
);

ALTER TABLE Employee
ADD Email VARCHAR(100);

ALTER TABLE Employee
ADD Salary DECIMAL(10,2);

ALTER TABLE Employee
MODIFY EmployeeName VARCHAR(150);

ALTER TABLE Employee
ADD CONSTRAINT UQ_Email UNIQUE (Email);

This example demonstrates several common ALTER TABLE operations performed during the lifecycle of a database application.


Conclusion

ALTER TABLE is one of the most powerful SQL commands because it allows database structures to evolve without recreating tables or losing data. As applications grow and requirements change, developers frequently use ALTER TABLE to add new columns, modify existing structures, create relationships, and improve data integrity.

By mastering ALTER TABLE operations, database professionals can efficiently maintain and enhance relational databases. Understanding when and how to use ALTER TABLE is essential for designing scalable, flexible, and maintainable database systems in real-world environments.

← Previous: Create Table Next: DROP Table →
Home Visit Our YouTube Channel