The Wayback Machine - https://web.archive.org/web/20240930205609/https://www.geeksforgeeks.org/sql-alter-rename/
Open In App

ALTER (RENAME) in SQL

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

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 SQL

Here we are discussing the syntax of Alter Command in different Databases like MYSQL, MariaDB, Oracle, etc. Let’s proceed with Syntax first.

Syntax:

ALTER TABLE table_name

RENAME TO new_table_name;

Columns can also be given a new name with the use of ALTER TABLE.

Query:

CREATE TABLE Student (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  age INT,
  email VARCHAR(50),
  phone VARCHAR(20)
);

Let’s insert some data and then perform ALTER operation to understand better bout alter command.

INSERT the data in Student Table

INSERT INTO Student (id, name, age, email, phone) 
VALUES 
(1, 'Amit', 20, 'amit@gmail.com', '9999999999'),
(2, 'Rahul', 22, 'rahul@yahoo.com', '8888888888'),
(3, 'Priya', 21, 'priya@hotmail.com', '7777777777'),
(4, 'Sonia', 23, 'sonia@gmail.com', '6666666666'),
(5, 'Kiran', 19, 'kiran@yahoo.com', '5555555555');

Output:

Student Table

Student Table

Example of ALTER (RENAME) in SQL

Example 1

Change the name of column name to FIRST_NAME in table Student. To change the column name of the existing table you have to use Column keyword before writing the existing column name to change

Syntax:

ALTER TABLE Student RENAME COLUMN Column_NAME TO FIRST_NAME;

Query:

ALTER TABLE Student RENAME Column name TO FIRST_NAME;

Output:

Output

Example 2

Change the name of the table Student to Student_Details.

Query:

ALTER TABLE Student RENAME TO Student_Details;

Output:

Student_Details Table

Student_Details table

Student_Details table

To Add a New Column with ALTER TABLE

To add a new column to the existing table, we first need to select the table with ALTER TABLE command table_name, and then we will write the name of the new column and its datatype with ADD column_name datatype. Let’s have a look below to understand better.

Syntax:

ALTER TABLE table_name

ADD column_name datatype;

Query:

ALTER TABLE Student ADD marks INT;

Output:

output

output

Conclusion

In conclusion, you can use ALTER Command whenever you want to change the data in an existing table like datatype from int to float and CHAR to VARCHAR, rename the table column name and add a new column, etc. In this article, we discussed all the Syntax of ALTER commands in the different databases with examples.


Previous Article
Next Article

Similar Reads

MongoDB - Rename Operator ($rename)
MongoDB $rename operator is a powerful tool for updating field names within documents which ensures data consistency and structure. This operator allows developers to rename fields in a MongoDB collection, including those nested within documents or arrays. In this article, We will learn about the MongoDB $rename by understanding various examples in
4 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
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 Server ALTER TABLE ADD Column
The ALTER TABLE ADD is a Data Definition Language (DDL) command that is used to alter the structure of the table by adding an extra column based on the new requirement. Using ALTER TABLE ADD we can also add new constraints and also indexes for the table. With the help of this command, We can simply apply modifications to the columns of our tables.
2 min read
SQL Server ALTER TABLE
In SQL Server, there are various commands to Add, Update, and Delete a Database Schema called DDL or Data Definition Language commands. A Table in a database is part of the database schema and the 'ALTER TABLE Moify Column' command is used to make changes to the database table column data type, column size, and if the column can accept NULL or NOT
5 min read
SQL Server ALTER TABLE DROP COLUMN
In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In this article, we will discuss how to drop a colum
4 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 ALTER TABLE
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 STATEMENTThe 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 constra
2 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
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
SQL Query to Rename Stored Procedure
Stored Procedure is a saved SQL code. It is created to save time as we can use it again and again without writing the whole query. In this article, we will see how to Rename a Stored Procedure. To create a Procedure:Syntax: CREATE PROCEDURE procedure_name AS SELECT * FROM table_name;SQL query whose procedure needs to be created is always written af
2 min read
How to Use SQL Query to Rename a Constraint?
In SQL, we sometimes need to rename the constraints of a table. The whole process for doing the same is demonstrated 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 create a database named GeeksForGeeks. Query: CREATE DATABASE GeeksForGeeksOutput: Step
2 min read
How to Rename SQL Server Schema?
In SQL, we cannot RENAME a SCHEMA. To achieve this, we need to create a new SCHEMA, transfer all the contents(objects) from the old schema to new schema and then finally delete the old schema using the DROP command. The same is depicted in the below article. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Displa
3 min read
SQL Server Rename Table
In SQL Server, renaming tables is a frequent operation that we often require during database maintenance or schema changes. This article ensures your seamless transition through the table-renaming process without compromising data integrity. it provides comprehensive guidance and guarantees protection for your valuable information – all in pursuit
2 min read
How to Rename a Column in PL/SQL?
In PL/SQL, renaming the column name plays a crucial role in the database management. It is performed the enhance clarity, maintain consistency, or accommodate evolving the requirements of the data. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column names. PL/SQL can provide the
3 min read
PL/SQL RENAME VIEW
PL/SQL which is Oracle Corporation's extension of SQL with a procedural language enables developers and database administrators to carry out complex operations in the databases. One of the frequently performed operations in the process of database maintenance is the change of names of objects to accomplish better organization and clarity. In this g
3 min read
SQL Query to Rename Database
To rename a database in SQL use the ALTER DATABASE Statement with MODIFY NAME clause. The ALTER command in SQL is used to make changes to a table or database. In this article, we will learn how to use the ALTER DATABASE statement with the MODIFY NAME clause to rename a database. How to Rename Database in SQL?To change the name of a database in SQL,
1 min read
How to Rename a Column in View in PL/SQL?
Renaming a column in a view can be a challenging task in PL/SQL, as the language does not provide a direct command for this operation. However, there are several effective methods to achieve this goal without compromising data integrity or the structure of the view. In This article, we will explore three key approaches for recreating the view, usin
4 min read
SQL - Rename View
In relational databases, views play the main role in simplifying complex queries, enhancing data security, and improving overall database management. So, the requirements of the database evolve to need to rename existing views to reflect their purpose better or to maintain the consistency in the database schema. Renaming the views in SQL is a funda
4 min read
Rename Column in SQL Server
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. Renaming a column in a database is a common task usually required when users want to change the database schema. In this article, we will explore different methods to rename columns in SQL Server. Rename Column in
3 min read
How to Rename a View in SQL Server?
The view is a virtual table based on the result set of an SQL statement. It is like the subset of the table and created to optimize the database experience. Like a real table, this also contains rows and columns. The data in a view are extracted from one or more real tables in the database. Renaming a View in MySQLWe can perform all the functions t
2 min read
Rename column SQL Server 2008
Renaming a column in a database is a common task that often arises when users want to change the database schema. In SQL Server 2008, renaming columns can be done efficiently using the sp_rename system-stored procedure, as the ALTER TABLE RENAME COLUMN syntax is not supported in this version. This article explains how to rename columns in SQL Serve
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
Article Tags :