How do you rename an existing table in SQL?
Posted by KarenKg
Last Updated: June 16, 2024
To rename an existing table in SQL, you can use the ALTER TABLE statement followed by the RENAME TO clause. The exact syntax can vary slightly depending on the SQL database you are using, but here's the general format:
Syntax
ALTER TABLE old_table_name RENAME TO new_table_name;
Example
If you have a table named employees that you want to rename to staff, you would use the following SQL command:
ALTER TABLE employees RENAME TO staff;
Database-Specific Notes
- PostgreSQL: The above syntax works directly in PostgreSQL. - SQLite: The same syntax is also valid in SQLite. - MySQL: In MySQL, the command is similar, but you might want to use RENAME TABLE instead:
RENAME TABLE old_table_name TO new_table_name;
- SQL Server: In SQL Server, you would use the sp_rename stored procedure:
EXEC sp_rename 'old_table_name', 'new_table_name';
Make sure you have the necessary permissions to alter the table, and check for dependencies like foreign keys or constraints that might be affected by renaming the table.