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.
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.Original table: Customers
CustomerID | FirstName | LastName | |
---|---|---|---|
1 | John | Doe | john.doe@email.com |
2 | Jane | Smith | jane.smith@email.com |
3 | Bob | Johnson | bob.johnson@email.com |
-- Rename the table
RENAME TABLE Customers TO Clients;
Updated table: Clients
CustomerID | FirstName | LastName | |
---|---|---|---|
1 | John | Doe | john.doe@email.com |
2 | Jane | Smith | jane.smith@email.com |
3 | Bob | Johnson | bob.johnson@email.com |
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;