sqlcmd.net validated sql reference
intermediate defining MySQL MariaDB SQL Server PostgreSQL SQLite

Rename a Table

Change a table name while keeping its existing rows and columns.

Docker-validated Not currently validation-green

Rename a draft table after review

The table name changes from draft_products to products. The stored rows are not copied or changed; they are simply read through the new table name.

MySQL MariaDB PostgreSQL SQLite
Engine-specific syntax
Setup
CREATE TABLE draft_products (id INT, name VARCHAR(50));

INSERT INTO
  draft_products
VALUES
  (1, 'Widget'),
  (2, 'Gadget');
SQL
ALTER TABLE draft_products
RENAME TO products;

SELECT
  id,
  name
FROM
  products
ORDER BY
  id;
idname
1Widget
2Gadget
SQL Server
Engine-specific syntax
Setup
CREATE TABLE draft_products (id INT, name VARCHAR(50));

INSERT INTO
  draft_products
VALUES
  (1, 'Widget'),
  (2, 'Gadget');
SQL
EXEC sp_rename 'draft_products',
'products';

SELECT
  id,
  name
FROM
  products
ORDER BY
  id;
idname
1Widget
2Gadget

Most supported engines use `ALTER TABLE ... RENAME TO`; SQL Server uses `sp_rename`.

Where this command helps.

  • renaming a staging table after it becomes the production table
  • cleaning up table names during a schema migration without rewriting the stored rows

What the command is doing.

Renaming a table updates the schema name used to reference it without copying data. It is useful during cleanup, migrations, and naming-standard changes. Because application queries and dependent objects may still refer to the old name, plan table renames carefully and update callers at the same time.