Rename table using RENAME or ALTER command

SQL

Renaming a table in SQL is a common operation that allows us to change the name of an existing table.

This can be useful for various reasons, such as improving naming conventions, reflecting changes in the dataset, or for organizational purposes.

Syntax:

The basic syntax for renaming a table using the RENAME command is as follows:

RENAME TABLE old_table_name TO new_table_name;
  • old_table_name: The current name of the table you want to rename.
  • new_table_name: The desired new name for the table.

Example:

Original table: Customers

CustomerIDFirstNameLastNameEmail
1JohnDoejohn.doe@email.com
2JaneSmithjane.smith@email.com
3BobJohnsonbob.johnson@email.com
-- Rename the table
RENAME TABLE Customers TO Clients;

Updated table: Clients

CustomerIDFirstNameLastNameEmail
1JohnDoejohn.doe@email.com
2JaneSmithjane.smith@email.com
3BobJohnsonbob.johnson@email.com

Using ALTER command

We cannot directly use the ALTER TABLE command to rename an existing table. The ALTER TABLE command is primarily used for modifying the structure of a table, such as adding or dropping columns.

However, we can use REANAME TO command to rename the Table.

ALTER TABLE old_table_name RENAME TO new_table_name;