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

SQL ALTER TABLE

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

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

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
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
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 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
SQL ALTER COLUMN
In today's data-driven world, successful organizations require the ability to process and manipulate data effectively as part of their business foundation. Structured Query Language (SQL) is a powerful tool that enables users to manipulate relational databases. Within SQL, there is an entity where data can be stored, retrieved, and manipulated. Thi
4 min read
SQL Query to Alter Column Size in MySQL
To Alter the Column size in MySQL use the ALTER TABLE statement with MODIFY Clause. The ALTER TABLE statement allows users to add, delete, or modify columns in an existing table in MySQL. SyntaxSyntax to Alter column size in MySQL is: ALTER TABLE table_nameMODIFY column_namevarchar(new_length); MySQL Alter Column Size in ExamplesLet us look at some
2 min read
Create, Alter and Drop schema in MS SQL Server
Schema management in MS SQL Server involves creating, altering, and dropping database schema elements such as tables, views, stored procedures, and indexes. It ensures that the database structure is optimized for data storage and retrieval. In this article, we will be discussing schema and how to create, alter, and drop the schema. What is Schema?A
4 min read
SQL Query to Drop Foreign Key Constraint Using ALTER Command
A foreign key is included in the structure of a table, to connect multiple tables by referring to the primary key of another table. Sometimes, you might want to remove the primary key constraint from a table for performance issues or other requirements. A foreign key constraint can be removed from a table using the ALTER TABLE command and DROP CONS
3 min read
ALTER SCHEMA in SQL Server
The SQL Server ALTER SCHEMA statement is used to transfer an object (table, view, or function) from one schema to another schema in the same database. The ALTER SCHEMA statement is used to transfer an object from a schema to another schema in the same database. SyntaxALTER SCHEMA target_schema_name TRANSFER [ object_type :: ] object_name;Parameters
2 min read
ALTER (RENAME) in SQL
Sometimes we may want to rename our table to give it a more relevant name. For this purpose, we can use ALTER TABLE to rename the name of the table. SQL ALTER TABLE is a command used to modify the structure of an existing table in a database. Here we will discuss Alter Command in SQL in detail. Alter in SQLHere we are discussing the syntax of Alter
2 min read
SQL Query to Add Foreign Key Constraints Using ALTER Command
In relational databases, a Foreign Key Constraint is a fundamental concept that defines a relationship between two tables. It ensures that the value of one or more columns in a child table corresponds to the value in a primary key or unique key in a parent table. In this article, we will look into how we can add a foreign key constraint using the A
3 min read
How to Alter a Column from Null to Not Null in SQL Server
In SQL Server, columns are defined with specific constraints, one of which is the nullability of the column whether or not it can hold NULL values. As a database evolves, this setting may need to be changed particularly to ensure that certain data fields are always populated. Altering a column from NULL to NOT NULL is a common task when you want to
4 min read
How to add a foreign key using ALTER in MySQL
In this article, we will discuss the overview of foreign keys and will discuss how to add a foreign key using ALTER in MySQL step by step. Let's discuss it one by one. Foreign key : If an attribute is a primary key in one table but was not used as a primary key in another table then the attribute which is not a primary key in the other table is cal
3 min read
How to Select All Records from One Table That Do Not Exist in Another Table in SQL?
We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries. In this let us see How to select All Records from One Table That Do Not Exist in Another Table step-by-step. Creating a Database Use the below command to create a database named Geeks
2 min read
SQL Query to Filter a Table using Another Table
In this article, we will see, how to filter a table using another table. We can perform the function by using a subquery in place of the condition in WHERE Clause. A query inside another query is called subquery. It can also be called a nested query. One SQL code can have one or more than one nested query. Syntax: SELECT * FROM table_name WHERE col
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Removing Duplicate Rows (Based on Values from Multiple Columns) From SQL Table
In SQL, some rows contain duplicate entries in multiple columns(>1). For deleting such rows, we need to use the DELETE keyword along with self-joining the table with itself. The same is illustrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to crea
3 min read
SQL | Checking Existing Constraints on a Table using Data Dictionaries
Prerequisite: SQL-Constraints In SQL Server the data dictionary is a set of database tables used to store information about a database's definition. One can use these data dictionaries to check the constraints on an already existing table and to change them(if possible). USER_CONSTRAINTS Data Dictionary: This data dictionary contains information ab
2 min read
What is Temporary Table in SQL?
Temporary Tables are most likely as Permanent Tables. Temporary Tables are Created in TempDB and are automatically deleted as soon as the last connection is terminated. Temporary Tables helps us to store and process intermediate results. Temporary tables are very useful when we need to store temporary data. The Syntax to create a Temporary Table is
2 min read
SQL Query to Display Last 5 Records from Employee Table
Summary : Here we will be learning how to retrieve last 5 rows from a database table with the help of SQL queries. The Different Approaches we are going to explore are : With the help of LIMIT clause in descending order.With the help of Relational operator and COUNT function.With the help of Prepared Statement and LIMIT clause. Creating Database :
4 min read
SQL | Query to select NAME from table using different options
Let us consider the following table named "Geeks" : G_ID FIRSTNAME LASTNAME DEPARTMENT 1 Mohan Arora DBA 2 Nisha Verma Admin 3 Vishal Gupta DBA 4 Amita Singh Writer 5 Vishal Diwan Admin 6 Vinod Diwan Review 7 Sheetal Kumar Review 8 Geeta Chauhan Admin 9 Mona Mishra Writer SQL query to write “FIRSTNAME” from Geeks table using the alias name as NAME.
2 min read
Table operations in MS SQL Server
In a relational database, the data is stored in the form of tables and each table is referred to as a relation. A table can store a maximum of 1000 rows. Tables are a preferred choice as: Tables are arranged in an organized manner. We can segregate the data according to our preferences in the form of rows and columns.Data retrieval and manipulation
2 min read
How to find first value from any table in SQL Server
We could use FIRST_VALUE() in SQL Server to find the first value from any table. FIRST_VALUE() function used in SQL server is a type of window function that results in the first value in an ordered partition of the given data set. Syntax : SELECT *, FROM tablename; FIRST_VALUE ( scalar_value ) OVER ( [PARTITION BY partition_value ] ORDER BY sort_va
2 min read
How to find last value from any table in SQL Server
We could use LAST_VALUE() in SQL Server to find the last value from any table. LAST_VALUE() function used in SQL server is a type of window function that results the last value in an ordered partition of the given data set. Syntax : SELECT *, FROM tablename LAST_VALUE ( scalar_value ) OVER ( [PARTITION BY partition_expression ] ORDER BY sort_expres
2 min read
Extract domain of Email from table in SQL Server
Introduction : As a DBA, you might come across a request where you need to extract the domain of the email address, the email address that is stored in the database table. In case you want to count the most used domain names from email addresses in any given table, you can count the number of extracted domains from Email in SQL Server as shown belo
3 min read
Capturing INSERT Timestamp in Table SQL Server
While inserting data in tables, sometimes we need to capture insert timestamp in the table. There is a very simple way that we could use to capture the timestamp of the inserted rows in the table. Capture the timestamp of the inserted rows in the table with DEFAULT constraint in SQL Server. Use DEFAULT constraint while creating the table: Syntax: C
2 min read
SQL Query to Display Last 50% Records from Employee Table
Here, we are going to see how to display the last 50% of records from an Employee Table in MySQL and MS SQL server's databases. For the purpose of demonstration, we will be creating an Employee table in a database called "geeks". Creating a Database : Use the below SQL statement to create a database called geeks: CREATE DATABASE geeks;Using Databas
2 min read
SQL Query to Display First 50% Records from Employee Table
Here, we are going to see how to display the first 50% of records from an Employee Table in MS SQL server's databases. For the purpose of the demonstration, we will be creating an Employee table in a database called "geeks". Creating a Database : Use the below SQL statement to create a database called geeks: CREATE DATABASE geeks;Using Database :US
2 min read
Article Tags :