×

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 COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

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 SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 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 SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 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.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.