SQL Server
EXEC sp_rename 'customers', 'clients';
Prints a caution that renaming can break scripts and procedures. Two sp_rename calls inside BEGIN TRAN … COMMIT swap names atomically.
Point a new name at the same table, and swap two tables' names without a gap.
Filed under Tables & schema
ALTER TABLE … RENAME TO works on PostgreSQL, MySQL, SQLite and Oracle; SQL Server needs sp_rename. For an atomic swap, use MySQL's multi-table RENAME TABLE or two renames in one transaction on SQL Server, PostgreSQL or SQLite. Oracle can't swap atomically.
Table renames only change metadata, so they are instant even on huge tables. For zero-downtime swaps, MySQL's RENAME TABLE takes several renames in one atomic statement; SQL Server, PostgreSQL and SQLite get the same effect by running two renames in one transaction.
EXEC sp_rename 'customers', 'clients';
Prints a caution that renaming can break scripts and procedures. Two sp_rename calls inside BEGIN TRAN … COMMIT swap names atomically.
ALTER TABLE customers RENAME TO clients;
Instant. Views and foreign keys keep working because they reference the table by OID. DDL is transactional, so two renames in one transaction swap names atomically.
RENAME TABLE customers TO clients;
Several renames in one statement are atomic: RENAME TABLE customers TO customers_old, customers_new TO customers swaps with no gap. ALTER TABLE … RENAME TO also works.
ALTER TABLE customers RENAME TO clients;
3.25+ also rewrites references in triggers and views; PRAGMA legacy_alter_table = ON restores the old behavior, which left them alone.
ALTER TABLE customers RENAME TO clients;
Indexes, grants and constraints follow the table. Views, synonyms and PL/SQL that use the old name become invalid. DDL commits implicitly, so there is no atomic two-table swap.
Yes, on every engine, including SQL Server's sp_rename: constraints reference the table internally, not by name. What breaks is code that names the table as text, such as views and procedures on SQL Server and Oracle.
Build customers_new, backfill it in batches, then swap: RENAME TABLE customers TO customers_old, customers_new TO customers in MySQL, or both renames in one transaction where DDL is transactional. Queries never see a moment where customers is missing.
PostgreSQL: ALTER TABLE customers SET SCHEMA archive. SQL Server: ALTER SCHEMA archive TRANSFER dbo.customers. MySQL: RENAME TABLE shop.customers TO archive.customers moves it to another database. Oracle has no rename across schemas: copy with CREATE TABLE … AS SELECT or Data Pump. SQLite can't move tables between attached databases.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.