SQL RENAME TABLE
Changes a table's name in place. The statement is easy; the interesting part is what happens to the objects that referenced the old name, and that answer differs more than you would expect.
- Categoria
- Tabelas
- In this category
- 4 of 16
Sintaxe
ALTER TABLE old_name RENAME TO new_name -- PostgreSQL, SQLite, Oracle, MySQL
RENAME TABLE old_name TO new_name -- MySQL, several tables in one statement
EXEC sp_rename 'dbo.old_name', 'new_name' -- SQL Server
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sim | EXEC sp_rename 'old', 'new'; there is no ALTER TABLE RENAME |
| PostgreSQL | Sim | views and foreign keys follow the rename automatically |
| MySQL | Sim | RENAME TABLE a TO b, c TO d renames several tables in one atomic statement |
| SQLite | Sim | since 3.25, references inside views and triggers are updated too |
| Oracle | Sim | ALTER TABLE old RENAME TO new, or the RENAME old TO new statement |
Exemplos
PostgreSQL
ALTER TABLE orders RENAME TO orders_archive;
MySQL
RENAME TABLE orders TO orders_archive, orders_new TO orders;
SQL Server
EXEC sp_rename 'dbo.orders', 'orders_archive';
SQLite
ALTER TABLE orders RENAME TO orders_archive;
Oracle
ALTER TABLE orders RENAME TO orders_archive;
Bom saber
- SQL Server's sp_rename changes the name but not the text of views, procedures or functions that mention it. Those modules bind by name, so after the rename they fail with Msg 208 (Invalid object name) until you edit them; only foreign keys, which bind by object id, follow the rename. Renaming a column breaks them the same way (Msg 207), a schema-bound view blocks the rename outright (Msg 15336), and every sp_rename prints a warning to this effect.
- PostgreSQL views and foreign keys follow the rename on their own, because they reference the table's internal id rather than its name. Functions are the exception: SQL and PL/pgSQL bodies written as strings keep the old name and fail with relation does not exist; only BEGIN ATOMIC function bodies are rewritten.
- SQLite 3.25+ updates references to the table inside views and triggers as part of the rename; older builds left them stale, and the legacy_alter_table pragma recreates that old behaviour. Apple's macOS build of sqlite3 turns that pragma on by default, so run PRAGMA legacy_alter_table = OFF there first.
- Oracle views and PL/SQL units that spell the old name go invalid on the rename and stay broken until you recreate or recompile them against the new name.
Questions about RENAME TABLE
Does renaming a table break views and stored procedures?
On SQL Server, MySQL and Oracle, yes: their definitions still spell the old name, so SQL Server views, procedures and functions fail with Invalid object name, MySQL views fail with error 1356 and procedures with 1146, and Oracle marks dependents invalid. PostgreSQL rewrites views and foreign keys (but not functions whose body is a string), and SQLite 3.25+ rewrites views and triggers.
How do I swap two table names?
MySQL takes it in one atomic statement: RENAME TABLE orders TO tmp, orders_new TO orders, tmp TO orders_new. SQL Server, PostgreSQL and SQLite run the same three renames inside one transaction, so other sessions never see a missing name. Oracle commits each DDL statement, so there you accept a brief window where one name is missing.
Why does sp_rename warn me?
Because it only changes the name. Foreign keys and other objects that bind by id keep working, but every view, procedure and function that stored the old name as text now fails with Invalid object name until you edit it. Search sys.sql_modules for the old name after renaming.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.