How to Rename Table Name in SQL?
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:
Name | Age | Class |
---|---|---|
Peter | 15 | 10 |
Bruce | 14 | 9 |
Tony | 15 | 9 |
Clark | 16 | 11 |
Loki | 13 | 8 |
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:
Name | Age | Class |
---|---|---|
Peter | 15 | 10 |
Bruce | 14 | 9 |
Tony | 15 | 9 |
Clark | 16 | 11 |
Loki | 13 | 8 |
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:
Name | Age | Class |
---|---|---|
Peter | 15 | 10 |
Bruce | 14 | 9 |
Tony | 15 | 9 |
Clark | 16 | 11 |
Loki | 13 | 8 |
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.