The Wayback Machine - https://web.archive.org/web/20250126163633/https://www.geeksforgeeks.org/sql-alter-add-drop-modify/
Open In App

SQL ALTER TABLE

Last Updated : 28 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The SQL ALTER TABLE statement is a powerful tool that allows you to modify the structure of an existing table in a database. Whether you’re adding new columns, modifying existing ones, deleting columns, or renaming them, the ALTER TABLE statement enables you to make changes without losing the data stored in the table.

In this article will learn about the ALTER TABLE statement with examples to demonstrate how it works.

SQL ALTER TABLE STATEMENT

The ALTER TABLE statement in SQL is used to add, remove, or modify columns in an existing table. The ALTER TABLE statement is also used to add and remove various constraints on existing tables.

It allows for structural changes like adding new columns, modifying existing ones, deleting columns, and renaming columns within a table.

Syntax:

To alter/modify the table use the ALTER TABLE syntax:

ALTER TABLE table_name
clause [column_name] [datatype];

Here, the clause is the operational clause of the ALTER TABLE statement. Some key clauses of the ALTER TABLE statement are:

Common Use Cases for SQL ALTER TABLE

1. ADD – To add a new column to the table

The ADD clause is used to add a new column to an existing table. You must specify the name of the new column and its data type.

Query:

ALTER TABLE table_name
ADD column_name datatype;

2. MODIFY/ALTER – To change the data type of an existing column

The MODIFY (or ALTER COLUMN in some databases like SQL Server) clause is used to modify the definition of an existing column, such as changing its data type or size.

Query:

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

3. DROP – To delete an existing column from the table

The DROP clause allows you to remove a column from a table. Be cautious when using this command as it will permanently remove the column and its data.

Query:

ALTER TABLE table_name
DROP COLUMN column_name;

4. RENAME COLUMN – To rename an existing column

You can rename an existing column using the RENAME COLUMN clause. This allows you to change the name of a column while preserving its data type and content.

Query:

ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;

5. RENAME TO – To rename the table itself

You can rename an entire table using the RENAME TO clause. This changes the name of the table while preserving its structure and data.

ALTER TABLE table_name
RENAME TO new_table_name;

SQL ALTER TABLE Examples

Below are the examples of ALTER TABLE statement. These examples demonstrates different use cases and shows how to use ALTER TABLE statement in SQL.

SQL ALTER TABLE ADD Column Example

The following SQL query adds an “Email” column to the “Students” table:

ALTER TABLE Students
ADD Email varchar(255);

SQL ALTER TABLE DROP Column Example

The following query deletes the “Email” column from “Students” table:

ALTER TABLE Students
DROP COLUMN Email;

SQL ALTER TABLE MODIFY Column Example

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

SQL ALTER TABLE Queries

Suppose there is a student database:

ROLL_NO NAME
1 Ram
2 Abhi
3 Rahul
4 Tanu

To ADD 2 columns AGE and COURSE to table Student.

Query:

 ALTER TABLE Student ADD 
 (AGE number(3),COURSE varchar(40));

Output: 

ROLL_NO NAME AGE COURSE
1 Ram    
2 Abhi    
3 Rahul    
4 Tanu    

MODIFY column COURSE in table Student.

Query:

 ALTER TABLE Student 
 MODIFY COURSE varchar(20);

After running the above query the maximum size of the Course Column is reduced to 20 from 40.

DROP column COURSE in table Student.

Query:

ALTER TABLE Student 
DROP COLUMN COURSE;

Output:

ROLL_NO NAME AGE
1 Ram  
2 Abhi  
3 Rahul  
4 Tanu  

Conclusion

The SQL ALTER TABLE statement is an essential tool for modifying the structure of an existing table. Whether you need to add, delete, or modify columns, or even rename the table or columns, ALTER TABLE provides the flexibility needed to manage your database schema efficiently. By following best practices and using this command wisely, you can maintain the integrity and performance of your database while making necessary structural changes.


Next Article

Similar Reads

three90RightbarBannerImg