Open In App

SQL ALTER TABLE

Last Updated : 26 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

SQL ALTER TABLE command can add, delete, or modify columns of an existing table.

This article discusses the SQL ALTER TABLE statement with examples and syntax.

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:

ADD – To add a new column to the table:

ALTER TABLE table_name
ADD column_name datatype;

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

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

DROP – To delete an existing column from the table:

ALTER TABLE table_name
DROP COLUMN column_name;

RENAME COLUMN – To rename an existing column:

ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;

RENAME TO – To rename the table itself:

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  


Previous Article
Next Article

Similar Reads

Modifying existing data in SQL : UPDATE and ALTER TABLE Command
To modify existing records in a table in SQL, use the UPDATE statement. The UPDATE command allows users to change a specific value in a table. However, this command cannot be used to modify the structure of a table. To modify the structure of an existing table, use the ALTER TABLE command. In this guide, we will learn how to modify existing data in
2 min read
Difference between ALTER and UPDATE Command in SQL
1. ALTER Command: ALTER SQL command is a DDL (Data Definition Language) statement. ALTER is used to update the structure of the table in the database (like add, delete, modify the attributes of the tables in the database). Syntax: // add a column to the existing table ALTER TABLE tableName ADD columnName columnDefinition; // drop a column from the
3 min read
Create, Alter and Drop schema in MS SQL Server
In this article, we will be discussing about schema and how to create, alter and drop the schema. 1. Create schema : A schema is usually a collection of objects. The objects can be tables, triggers, views, procedures etc. A database may have one or more schemas. SQL Server provides a feature of pre-defined schemas. The names of pre-defined schemas
3 min read
Alter login in SQL Server
Alter login statements can be used to changes the properties of a SQL Server login account. Syntax: ALTER LOGIN loginname; GO Enable a disabled login - Syntax : ALTER LOGIN loginname ENABLE; Example - ALTER LOGIN geeks ENABLE; Change password of a login Syntax : ALTER LOGIN geeks WITH PASSWORD = 'newpassword'; Example - ALTER LOGIN geeks WITH PASSW
1 min read
ALTER SCHEMA in SQL Server
The ALTER SCHEMA statement used to transfer a object from a schema to another schema in the same database. Syntax : ALTER SCHEMA target_schema_name TRANSFER [ object_type :: ] object_name; Parameters : target_schema_name is the schema in the database, into which the object will be moved. object_type represents the type of the object for which the o
2 min read
SQL Query to Drop Foreign Key Constraint Using ALTER Command
Here, we are going to see How to Drop a Foreign Key Constraint using ALTER Command(SQL Query) using Microsoft SQL Server. A Foreign key is an attribute in one table which takes references from another table where it acts as the primary key in that table. Also, the column acting as a foreign key should be present in both tables. Creating a new Datab
2 min read
SQL Query to Drop Unique Key Constraints Using ALTER Command
Here, we see how to drop unique constraints using alter command. ALTER is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Syntax : ALTER TABLE table_name DROP CONSTRAINT unique_constraint; For instance, consider the below table 'Employee'. Create a Table: C
2 min read
SQL Query to Add Foreign Key Constraints Using ALTER Command
In this article, we will look into how you can add a foreign key constraint using the ALTER command in SQL. For this article, we will be using Microsoft SQL Server as our database. But before we start let's take a look at the below terminologies: Primary key: It is used to uniquely identify the records in the table. It is mainly used in Relational
2 min read
SQL Query to Add Unique key Constraints Using ALTER Command
Here we will see how to add unique key constraint to a column(s) of a MS SQL Server's database with the help of a SQL query using ALTER clause. For the demonstration purpose, we will be creating a demo table in a database called "geeks". Creating the Database : Use the below SQL statement to create a database called geeks: CREATE DATABASE geeks; Us
2 min read
How to Alter Multiple Columns at Once in SQL Server?
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For t
3 min read
Article Tags :