DBAtlas · Database operations atlas

Rename a table / object

Oracle

-- Table
ALTER TABLE old_name RENAME TO new_name;
-- or
RENAME old_name TO new_name;
-- Column
ALTER TABLE customer RENAME COLUMN name TO full_name;

RENAME or ALTER ... RENAME TO is used for tables/views/sequences. Dependent objects (views, synonyms) may become invalid and need to be recompiled.

PostgreSQL

ALTER TABLE old_name RENAME TO new_name;
ALTER TABLE customer RENAME COLUMN name TO full_name;
ALTER VIEW old_view RENAME TO new_view;

ALTER ... RENAME TO works for most object types. Foreign key and index references are preserved automatically even when the name changes (OID-based).

MySQL

RENAME TABLE old_name TO new_name;
-- or
ALTER TABLE old_name RENAME TO new_name;
-- Column (8.0+)
ALTER TABLE customer RENAME COLUMN name TO full_name;

RENAME TABLE is atomic and can move multiple tables in a single statement. For renaming columns use RENAME COLUMN in 8.0+; before that, CHANGE must be used and the type specified as well.

SQL Server

-- Table
EXEC sp_rename 'dbo.old_name', 'new_name';
-- Column
EXEC sp_rename 'dbo.customer.name', 'full_name', 'COLUMN';

SQL Server has no RENAME TABLE statement; the system procedure sp_rename is used. The schema prefix is given in the table name but not in the new name. Dependent views/procedures must be updated manually.

MongoDB

// Rename a collection
db.old_name.renameCollection("new_name");
// or the admin command
db.adminCommand({ renameCollection: "db.old_name", to: "db.new_name" });

renameCollection changes the collection name. To rename a field, use the $rename operator with updateMany. Sharded collections can be renamed in 5.0+; older versions do not support it.

Redis

# Rename a key
RENAME old_key new_key
# Without overwriting if the target exists
RENAMENX old_key new_key

RENAME renames the key; if the target already exists, it is overwritten. RENAMENX works only if the target does not exist. An error is returned if the source key does not exist.

Elasticsearch

# An index cannot be renamed directly; reindex + alias approach
POST _reindex
{ "source": { "index": "old_name" }, "dest": { "index": "new_name" } }
DELETE old_name
# or use an alias for a seamless switch
POST _aliases
{ "actions": [ { "add": { "index": "new_name", "alias": "customer" } } ] }

There is no index rename command. Reindex to the new name with _reindex and delete the old one. Running behind a stable alias is recommended to keep the application independent of index names.

Open in DBAtlas →