How do you rename a column or a table, and what happens to the things that depend on them?
Both operations are metadata changes, and both are fast on every modern engine — no rows are rewritten, because the data does not move. A renamed column keeps its data and its position; a renamed table keeps its rows, its indexes and its constraints.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
full_name VARCHAR(120) NOT NULL,
email VARCHAR(200) NOT NULL
);
INSERT INTO customers (id, full_name, email) VALUES (1, 'Ada Lovelace', '[email protected]');
-- Rename a column.
ALTER TABLE customers RENAME COLUMN full_name TO name;
-- Rename the table.
ALTER TABLE customers RENAME TO clients;
SELECT id, name, email FROM clients;One row comes back: id 1, name 'Ada Lovelace', email [email protected]. Nothing was copied and nothing was lost.
What does change is everything that referred to the old name. In SQLite, renaming a table automatically updates the views and triggers that referenced it, and it rewrites the REFERENCES clauses of foreign keys pointing at it. Renaming a column does the same for views and triggers that use it. In PostgreSQL, the dependency tracking is more sophisticated: a rename does not break dependent views, because the view stores its dependency on the column's identity rather than its name. MySQL is the strictest of the three and will refuse to rename a table that a view depends on unless you drop the view first.
The naming rules are worth knowing. RENAME COLUMN and RENAME TO are the SQL standard forms and are supported by SQLite from 3.25, PostgreSQL, MySQL and SQL Server. Older MySQL syntax used CHANGE COLUMN, which required repeating the whole column definition — a spelling quirk worth remembering if you see it in an old script. SQLite also supports the older RENAME TO for tables, which is the same as RENAME TO.