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

ALTER (RENAME) in SQL

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

In SQL, making structural changes to a database is often necessary. Whether it’s renaming a table or a column, adding new columns, or modifying data types, the SQL ALTER TABLE command plays a critical role. This command provides flexibility to manage and adjust database schemas without affecting the existing data.

In this article, we will explain how to rename tables and columns in SQL using the ALTER TABLE command, along with practical examples. We’ll also cover how these commands vary across databases like MySQL, MariaDB, and Oracle. This comprehensive guide ensures our queries are accurate, efficient, and SEO-optimized.

What is the ALTER Command in SQL?

The structure of an existing table can be changed by users using the SQL ALTER TABLE command. If we need to rename a table, add a new column, or change the name of an existing column in SQL, the ALTER command is crucial for making schema changes without affecting the data that is already there. This is essential for tasks like:

  • Renaming a table.
  • Changing a column name.
  • Adding or deleting columns.
  • Modifying the data type of a column.

Syntax

1. Renaming a Table

ALTER TABLE table_name
RENAME TO new_table_name;

2. Renaming a Column

ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

3. Adding a New Column

ALTER TABLE table_name
ADD column_name datatype;

4. Modifying a Column Data Type

ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype;

Examples of ALTER Command in SQL

Below are practical examples to help us understand how to use the ALTER command effectively in various scenarios. These examples includes renaming tables or columns, adding new columns, or changing column data types.

1. Create a Sample Table

First, let’s create a sample table to demonstrate the ALTER command:

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

2. Insert Data into the Table

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

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 1: Rename a Column

Change the name of column name to FIRST_NAME in table Student. To change the column name of the existing table we 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: Rename a Table

In this example, the table name Student is changed to Student_Details using the ALTER TABLE command, making the name more descriptive and relevant to its content.

Query:

ALTER TABLE Student RENAME TO Student_Details;

Output

Student_Details table

Student_Details table

Example 3: Add a New Column

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

5. Modify a Column Data Type

In the example, the phone column is updated from VARCHAR(20) to BIGINT to store numerical data more efficiently and ensure data integrity for phone numbers without unnecessary characters.

Syntax

ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype;

Query:

ALTER TABLE Student_Details
MODIFY COLUMN phone BIGINT;

Output

id name age email phone
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

Explanation:

  • The phone column now has a BIGINT data type, suitable for storing large numeric values.
  • Existing data remains unchanged but is stored as integers instead of strings.

Conclusion

The SQL ALTER TABLE command is an effective way to modify the structure of an already-existing tables in a database. When necessary, we can use ALTER TABLE to rename the entire table, rename a specific column in SQL, or change a column name to something more descriptive. This command is important for database administration since it also enables the addition of new columns and the modification of data types.

FAQs

How do you rename using ALTER in SQL?

You can rename a table or column in SQL using the ALTER command with the RENAME keyword. To rename a table: ALTER TABLE old_table_name RENAME TO new_table_name;
To rename a column: ALTER TABLE table_name RENAME COLUMN old_column_name
TO new_column_name;

How do I rename a SQL name?

To rename a database object like a table, column, or index in SQL, use the ALTER statement. For tables: ALTER TABLE old_table_name RENAME TO new_table_name;

How do you rename items in SQL?

You can rename SQL items (tables, columns, etc.) using the ALTER statement. For instance, to rename a column: ALTER TABLE table_name RENAME COLUMN old_column TO new_column; Different databases (like MySQL, SQL Server, PostgreSQL) may have variations in syntax.



Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg