Example 1
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.
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;| id | name |
|---|---|
| 1 | Widget |
| 2 | Gadget |
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;| id | name |
|---|---|
| 1 | Widget |
| 2 | Gadget |
Most supported engines use `ALTER TABLE ... RENAME TO`; SQL Server uses `sp_rename`.