×

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only while using the ALTER TABLE statement. The ALTER TABLE statement also permits users to add or drop the SQL constraints on the existing tables in the SQL.

The ALTER TABLE statement also permits the user to rename the existing table.

ALTER TABLE ADD COLUMN statement in SQL

This is used when you need to add columns to the existing table. In such situation, rather than creating a new table, you can add a column in an existing table using ADD keyword.

Syntax of ALTER TABLE ADD COLUMN statement in SQL

ALTER TABLE Table_Name ADD Column_Name Data_Type;   

The above syntax is used to add a column in an existing table, and allows the users to add only a single field in the table.

To add more than one column in an existing table, use the following syntax:

ALTER TABLE Table_Name ADD (Column_Name1 Data_Type, Column_Name2 Data_Type);

Let's understand the ALTER TABEL ADD COLUMN in SQL with examples.

Example of ALTER TABLE ADD COLUMN in SQL

We have taken multiple SQL examples, which will help you understand adding a single column and multiple columns in the existing table using ALTER TABLE ADD COLUMN statement.

Consider the following tables along with the given records.

Table 1: Mobile_Plan:

Package_IdData_IdTalktime_Id
121
232
313

Table 2: Data_Plan:

Data_IdData_LimitData_Price
1230
2570
3455

Table 3: Talktime_Plan:

Talktime_IdTalktime _LimitTalktime _Price
1100150
270105
36090

Table 4: Customer (Empty Table)

Example 1: Write a query to add a new column Extra_Data_Price in the Data_Plan Table.

ALTER TABLE Data_Plan ADD Extra_Data_Price int; 

We have used the ALTER TABLE ADD statement in the above query to add a new Extra_Data_Price column in the Data_Plan table. We have added a single column in the existing table.

We will now use the DESC keyword and the table name to cross-check whether the Extra_Data_Price column was added or not successfully in the Data_Plan table.

DESC Data_Plan;   
FieldTypeNullKeyDefaultExtra
Data_Idint(11)NOPRINULL
Data_Limitint(11)YESNULL
Data_Priceint(11)YESNULL
Extra_Data_PriceInt(11)YES

Example 2: Write a query to add multiple columns CustomerId, Customer_Name, and Phone_Number in the Customer table, which is empty, having no columns.

ALTER TABLE Customer ADD (CustomerId int, Customer_Name varchar(20), Phone_Number varchar(10));

We have used the ALTER TABLE ADD statement in the above query to add multiple new columns CustomerId, Customer_Name, and Phone_Number in the Customer table. We have added multiple columns in the existing table.

We will now use the DESC keyword and the table name to cross-check whether the CustomerId, Customer_Name, and Phone_Number columns were added successfully or not in the Customer table.

DESC Customer;  
FieldTypeNullKeyDefaultExtra
CustomerIdint(11)YESNULL
Customer_Namevarchar(20)YESNULL
Phone_Numbervarchar(10)YESNULL

Suppose you want to add constraints like Primary Key, Foreign Key to an existing table. We will take an example of adding Primary Key constraints and Foreign Key constraints to the existing table. We can also add those constraints using ALTER TABLE ADD statement.

Example 3: Write a query to add Primary Key constraint on the column name CustomerId in the Customer Table.

ALTER TABLE Customer ADD Primary Key(CustomerId);

In the above query, we have used ALTER TABLE ADD statement to add the Primary Key constraint on the column name CustomerId in the Customer Table. We have used the Primary Key keyword with ALTER TABLE ADD statement to add a constraint on the CustomerId.

We will now use the DESC keyword and the table name to cross-check whether the CustomerId column key is changed to PRI in the Customer table.

DESC Customer;  
FieldTypeNullKeyDefaultExtra
CustomerIdint(11)YESPRINULL
Customer_Namevarchar(20)YESNULL
Phone_Numbervarchar(10)YESNULL

Example 4: Write a query to add Foreign Key constraint on the column name Package_Id in the Customer Table.

ALTER TABLE Customer ADD (Package_Id int, FOREIGN KEY(Package_Id) references Mobile_Plan(Package_ID));

In the above query, we have first added the Package_Id column in the Customer table, and then we have used Foreign Key on the same column Package_Id in the same query using ALTER TABLE ADD statement.

We will now use the DESC keyword and the table name to cross-check whether the Package_Id column was added or not successful in the Customer table and key change to MUL.

DESC Customer;   
FieldTypeNullKeyDefaultExtra
CustomerIdint(11)YESPRINULL
Customer_Namevarchar(20)YESNULL
Phone_Numbervarchar(10)YESNULL
Package_IdInt(11)YESMULNULL

ALTER TABLE MODIFY COLUMN statement in SQL

Suppose you want to update the column name or definition like a data type. In that case, we will use ALTER TABLE MODIFY statement to update the column data type of the already existing table. We will use MODIFY keyword just after the Table_Name in the statement.

Syntax of ALTER TABLE MODIFY COLUMN statement in SQL

ALTER TABLE Table_Name MODIFY Column_Name Data_Type;   

The above syntax is used to modify columns in an existing table allows the user to modify only a single field.

To modify more than one column in an existing table-use below syntax:

ALTER TABLE Table_Name MODIFY (Column_Name1 Data_Type, Column_Name2 Data_Type);

Let's understand the ALTER TABEL MODIFY in SQL with examples.

Example of ALTER TABLE MODIFY column in SQL

We have taken multiple SQL examples to help you understand modifying single columns and multiple columns in the existing table using ALTER TABLE MODIFY statement.

Consider the following tables along with the given records.

Table 1: Mobile_Plan:

Package_IdData_IdTalktime_Id
121
232
313

Table 2: Data_Plan:

Data_IdData_LimitData_Price
1230
2570
3455

Table 3: Talktime_Plan:

Talktime_IdTalktime _LimitTalktime _Price
1100150
270105
36090

Table 4: Customer

CustomerIdCustomer_NamePhone_NumberPackage_Id
101Bhavesh98465220211
102Mahesh77985982723
103Anita78652220211

Example 1: Write a query to update the size of the column Customer Name from the Customer Table.

ALTER TABLE Customer MODIFY Customer_Name varchar(30);

In the above query, we have modified the column Customer_Name size from 20 to 30. We have used ALTER TABLE statement with MODIFY keyword just after the table name to modify the column.

We will now use the DESC keyword and the table name to cross-check whether the column Customer_Name data type is modified in the Customer table.

DESC Customer;  
FieldTypeNullKeyDefaultExtra
CustomerIdint(11)YESPRINULL
Customer_Namevarchar(30)YESNULL
Phone_Numbervarchar(10)YESNULL
Package_IdInt(11)YESMULNULL
SQL ALTER TABLE

Example 2: Write a query to update the data type of the column Talktime_Limit and Talktime_Price from the Talktime_Plan Table.

ALTER TABLE Talktime_Plan MODIFY (Talktime_Limit varchar(2), Talktime_Price varchar(2));

We have modified the column Talktime_Limit and Talktime_Price data type from int to varchar in the above query. We have used ALTER TABLE statement with MODIFY keyword just after the table name to modify the column.

We will now use the DESC keyword and the table name to cross-check whether the column Talktime_Limit and Talktime_Price data type are modified or not in the Talktime_Plan table.

DESC Talktime_Plan; 
FieldTypeNullKeyDefaultExtra
TalkTime_Idint(11)YESPRINULL
TalkTime_Limitvarchar(2)YESNULL
TalkTime_Pricevarchar(2)YESNULL
SQL ALTER TABLE

ALTER TABLE DROP COLUMN statement in SQL

This is used when you delete the column from the existing table. Then in such a situation, rather than drop the entire table from the database, we can use the DROP keyword to drop the column.

Syntax of ALTER TABLE DROP COLUMN statement in SQL

ALTER TABLE Table_Name DROP Column_Name;   

Let's understand the ALTER TABEL DROP in SQL with examples.

Example of ALTER TABLE DROP column in SQL

We have taken SQL examples to help you understand modifying single columns and multiple columns in the existing table using ALTER TABLE DROP statement.

Consider the following tables along with the given records.

Table 1: Mobile_Plan:

Package_IdData_IdTalktime_Id
121
232
313

Table 2: Data_Plan:

Data_IdData_LimitData_Price
1230
2570
3455

Table 3: Talktime_Plan:

Talktime_IdTalktime _LimitTalktime _Price
1100150
270105
36090

Table 4: Customer

CustomerIdCustomer_NamePhone_NumberPackage_Id
101Bhavesh98465220211
102Mahesh77985982723
103Anita78652220211

Example 1: Write a query to drop the Package_Id from the Customer table.

ALTER TABLE Customer DROP COLUMN Package_Id;

We have dropped the column name Package_Id from the Customer table in the above query. We have used ALTER TABLE DROP statement to drop the column from the table.

We will now cross_check whether the column Package_Id from the Customer table was deleted or not successfully.

SELECT * FROM CUSTOMER;
CustomerIdCustomer_NamePhone_Number
101Bhavesh9846522021
102Mahesh7798598272
103Anita7865222021
SQL ALTER TABLE

ALTER TABLE RENAME statement in SQL

Suppose we want to change the name of the column or table, then we will use RENAME keyword to rename the fields or tables of the existing table.

Syntax of ALTER TABLE RENAME statement in SQL

ALTER TABLE Table_Name RENAME Column_Name;   

Let's understand the ALTER TABEL RENAME in SQL with examples.

Example of ALTER TABLE RENAME column in SQL

We have taken SQL examples to help you understand modifying single columns and multiple columns in the existing table using ALTER TABLE RENAME statement.

Consider the following tables along with the given records.

CustomerIdCustomer_NamePhone_Number
101Bhavesh9846522021
102Mahesh7798598272
103Anita7865222021

Example 1: Write a query to change the name of the Customer table.

ALTER TABLE Customer RENAME TO CUST;

In the above query, we have renamed the table name of the Customer table to the Cust table. We used ALTER TABLE RENAME statement to rename the table name.

We will cross-check whether the table's name is changed or not successfully.

SHOW TABLES;
SQL ALTER TABLE

Example 1: Write a query to change the name of the column CustomerId to cid from the Cust table.

ALTER TABLE Cust RENAME COLUMN CustomerId cid int not null;

In the above query, we have changed the column name CustomerId to Cid. We have used ALTER TABLE RENAME statement to rename the table name.

We will cross-check whether the table's name is changed or not successfully.

SELECT * FROM CUST; 
SQL ALTER TABLE

Related Topics

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.