Skip to content

03 SQL ‐ Rename Table

Pankaj Chouhan edited this page Sep 29, 2023 · 1 revision

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:

  1. 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 and new_table_name with the desired new name for the table.

  2. PostgreSQL:

    In PostgreSQL, you can use the ALTER TABLE statement with the RENAME TO clause:

    ALTER TABLE old_table_name RENAME TO new_table_name;

    Replace old_table_name with the current name of the table and new_table_name with the new name you want.

  3. 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 and new_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.

Clone this wiki locally