How to Rename Table Name in SQL?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Suppose we have two tables in our database named table1 and table2. Although we have an idea about the contents of table1 and table2 but anyone who does not know the internal structure of the database will have a hard time guessing what kind of data both tables have. Thus, it is a requirement to rename these tables.

We can rename table name in SQL by using the following ways::

  • RENAME table statement.
  • ALTER TABLE statement.
  • sp_rename procedure.

RENAME table Statement in SQL

The RENAME TABLE statement can be used to rename table name in SQL. Here we use the RENAME keyword followed by the old name of the table followed by the To keyword followed by the new name of the table.

Syntax

Example

Imagine we have a table of students containing their name, age, and class. The table would look like the following:

Students Table:

NameAgeClass
Peter1510
Bruce149
Tony159
Clark1611
Loki138

Now imagine this table is named table1. In this example, we will change its name to studentInformation.

ALTER TABLE Statement in SQL

The ALTER TABLE statement is used to rename table name in SQL. Here we use the ALTER TABLE keyword followed by the old name of the table followed by the RENAME TO keyword followed by the new name of the table.

Syntax

Example

Imagine we have a table of students containing their name, age, and class. The table would look like the following:

Students Table:

NameAgeClass
Peter1510
Bruce149
Tony159
Clark1611
Loki138

Now imagine this table is named table1. In this example, we will change its name to studentInformation.

Renaming a Table in SQL Server (sq_rename in SQL)

The sp_rename is also used to rename table name in SQL. Here we use the sp_rename keyword followed by the old name and the new name of the table separated by a comma.

Syntax

Example

Imagine we have a table of students containing their name, age, and class. The table would look like the following:

Students Table:

NameAgeClass
Peter1510
Bruce149
Tony159
Clark1611
Loki138

Now imagine this table is named table1. In this example, we will change its name to studentInformation.

Conclusion

  • SQL offers multiple methods to rename table name in SQL: RENAME table statement, ALTER TABLE statement, and sp_rename procedure.
  • These methods provide flexibility in renaming tables according to specific requirements.
  • Renaming tables can enhance clarity and organization within a database, improving readability and maintenance.
  • Whether using RENAME TABLE, ALTER TABLE, or sp_rename, SQL users have versatile options to manage table names efficiently.