-
Notifications
You must be signed in to change notification settings - Fork 14
03 SQL ‐ Rename Table
In SQL, you can rename a table using the ALTER TABLE
statement, but the syntax for renaming tables can vary between different database management systems. Below are examples for some common database systems:
-
MySQL and MariaDB:
To rename a table in MySQL or MariaDB, you can use the
RENAME TABLE
statement:RENAME TABLE old_table_name TO new_table_name;
Replace
old_table_name
with the current name of the table you want to rename andnew_table_name
with the desired new name for the table. -
PostgreSQL:
In PostgreSQL, you can use the
ALTER TABLE
statement with theRENAME TO
clause:ALTER TABLE old_table_name RENAME TO new_table_name;
Replace
old_table_name
with the current name of the table andnew_table_name
with the new name you want. -
SQL Server:
In SQL Server, you can use the
sp_rename
system procedure to rename a table:EXEC sp_rename 'old_table_name', 'new_table_name';
Replace
old_table_name
with the current name of the table andnew_table_name
with the new name you want.
Remember to exercise caution when renaming tables, as it can affect other database objects and queries that reference the table. Make sure you have appropriate permissions to perform this operation, and it's a good practice to back up your database before making such changes.