In SQL, most RDBMSs allow you to rename a table using the ALTER TABLE
statement, which is the statement used to modify the definition of an existing table.
Some RDBMs also include a RENAME
statement for renaming tables.
But if you use SQL Server, you’ll need to use the sp_rename
stored procedure.
PostgreSQL, MySQL, MariaDB, SQLite, Oracle
The ALTER TABLE
statement is the most widely supported method for renaming a table across the major RDBMSs. When using this statement, use the RENAME
clause to rename the table.
Example:
ALTER TABLE t1 RENAME TO t2;
This renames a table from t1
to t2
.
This should work in most RDBMs, including PostgreSQL, MySQL, MariaDB, SQLite, and Oracle.
This method does not work in SQL Server though.
SQL Server
In SQL Server, you’ll need to use the sp_rename
stored procedure.
Example:
EXEC sp_rename 't1', 't2';
You can also qualify the first table with the schema name, in which case, it might look something like this:
EXEC sp_rename 'dbo.t1', 't2';
In this example, dbo
is the schema name, but you will need to use whatever schema is applicable.
MySQL & MariaDB
In addition to the ALTER TABLE
method, MySQL and MariaDB also have a RENAME TABLE
statement.
Example:
RENAME TABLE
t1 TO t2,
t3 TO t4,
t5 TO t6;
Oracle
In addition to the ALTER TABLE
statement, Oracle has a RENAME
statement.
Example:
RENAME t1 TO t2;
Note that you cannot rename a sharded table or a duplicated table.