SQL: Structured Query LanguageSQL FundamentalsTable and Column Renaming
How do you rename a table or column?
Question 18 of 30 · Updated
On this page
- 1. Rename an existing table
- 2. Rename an existing column
- 3. Tested example: create, rename and verify
- 4. What actually changes?
- 5. Verify the new names
- 6. What happens to foreign keys and views?
- 7. SQL dialect differences
- 8. Transactions and rollback
- 9. Safe renaming checklist
- 10. Interview-ready answer
- Common follow-up questions
- Validation and references
To rename an existing SQL table or column, use the ALTER TABLE statement with the appropriate RENAME clause in databases such as PostgreSQL and modern SQLite. A rename changes an object's name rather than creating a replacement table or copying its rows. The underlying table data and the renamed column's values normally remain intact. However, application queries and other dependencies that refer to the old name may need to be updated.
1. Rename an existing table#
PostgreSQL and modern SQLite support the following syntax:
SQLALTER TABLE old_table_name RENAME TO new_table_name;
For example:
SQLALTER TABLE products RENAME TO catalog_items;
After the command succeeds, the table is called catalog_items. Queries that directly refer to the old table name products no longer work unless another object or compatibility layer provides that name. Existing rows do not need to be inserted again.
PostgreSQL allows the source table to be schema-qualified, for example ALTER TABLE inventory.products RENAME TO catalog_items;. The new name is not schema-qualified in this syntax: the rename stays within the existing schema. Moving a PostgreSQL table between schemas is a different operation (ALTER TABLE ... SET SCHEMA ...). The schema-qualified example is explanatory and was not run.
2. Rename an existing column#
SQLALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
For example:
SQLALTER TABLE catalog_items RENAME COLUMN label TO item_name;
The values previously stored in label are now accessed through item_name. Renaming does not change the column's type, default, nullability or intended meaning. If the data itself needs conversion, use an appropriate migration or UPDATE instead. PostgreSQL also permits omitting the optional keyword COLUMN in this form, but the explicit spelling is clearer and portable across more implementations.
3. Tested example: create, rename and verify#
Run the following example in a fresh, disposable database. The entire sequence was executed in an isolated in-memory SQLite 3.46.1 database. The syntax is also suitable for PostgreSQL, but PostgreSQL execution of these DDL statements was not performed in this session.
SQLCREATE TABLE products ( product_id INTEGER PRIMARY KEY, label TEXT NOT NULL ); INSERT INTO products VALUES (1, 'Notebook'), (2, 'Pen'); ALTER TABLE products RENAME TO catalog_items; SELECT * FROM catalog_items ORDER BY product_id; ALTER TABLE catalog_items RENAME COLUMN label TO item_name; SELECT product_id, item_name FROM catalog_items ORDER BY product_id;
Immediately after renaming the table, the query returns:
| product_id | label |
|---|---|
| 1 | Notebook |
| 2 | Pen |
After renaming the column, the query returns:
| product_id | item_name |
|---|---|
| 1 | Notebook |
| 2 | Pen |
The row count and values have not changed. The tested SQLite example also confirmed that the obsolete references SELECT * FROM products; and SELECT label FROM catalog_items; fail with missing-table and missing-column errors respectively. In a PostgreSQL transaction, errors should be handled through rollback or appropriate savepoint recovery; do not deliberately run error-producing statements in a production migration.
4. What actually changes?#
Renaming is a data definition language (DDL) operation. Conceptually, it changes the name associated with an existing database object. PostgreSQL normally performs a table or column rename through catalog changes without a table-data rewrite. That does not make the operation risk-free: locks may be needed, and dependent applications can fail when their expected names change.
| Property | Effect of an ordinary rename |
|---|---|
| Existing rows and values | Preserved |
| Table or column name | Changed |
| Data type | Unchanged by the rename itself |
| Primary key and constraints | Remain associated with the object, subject to engine-specific rules |
| Index and constraint names | Do not assume their own names are renamed automatically |
| External SQL and application code | May need updates |
5. Verify the new names#
In SQLite, the following introspection command was tested after the rename:
SQLPRAGMA table_info(catalog_items);
The column names returned were product_id and item_name. PRAGMA is SQLite-specific. PostgreSQL offers a portable information-schema-style query:
SQLSELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'catalog_items' ORDER BY ordinal_position;
This PostgreSQL example is illustrative: it assumes that the demonstration table is in the public schema and that the querying role can see it. Change the schema filter as appropriate. It was not executed against a production table. For a real migration, compare expected metadata, row counts and representative values using an approved test environment, then run application integration tests.
6. What happens to foreign keys and views?#
Database-managed dependencies and external references are not the same thing. In PostgreSQL, catalog-tracked dependencies such as ordinary foreign keys and views generally continue to reference the renamed relation or attribute by internal identity. The view's own exposed column name can remain different from the renamed source column. Function bodies containing SQL text, dynamic SQL, application code, ORM mappings, ETL jobs and BI queries may still contain the old name. Test rather than assuming every dependency is rewritten.
To demonstrate dependency handling in modern SQLite, run this independent variation in a fresh database. Enable foreign keys before creating the tables. The following setup and renames were tested on SQLite 3.46.1:
SQLPRAGMA foreign_keys = ON; CREATE TABLE products ( product_id INTEGER PRIMARY KEY, label TEXT NOT NULL ); INSERT INTO products VALUES (1,'Notebook'), (2,'Pen'); CREATE TABLE stock_events ( event_id INTEGER PRIMARY KEY, product_id INTEGER REFERENCES products(product_id) ); INSERT INTO stock_events VALUES (1,1); CREATE VIEW product_labels AS SELECT product_id, label FROM products; ALTER TABLE products RENAME TO catalog_items; ALTER TABLE catalog_items RENAME COLUMN label TO item_name; PRAGMA foreign_key_list(stock_events); PRAGMA foreign_key_check; SELECT * FROM product_labels ORDER BY product_id;
The foreign-key metadata referred to catalog_items, the foreign-key integrity check returned no violations, and the view still returned the two original rows. These are observations for the tested SQLite version and settings; older SQLite releases and legacy ALTER TABLE behavior can differ. The example does not establish PostgreSQL's exact view output-column naming or behavior for every dependency type.
7. SQL dialect differences#
Renaming syntax is not identical in all SQL products. Use the syntax documented for the deployed engine and version.
| Engine | Rename table | Rename column |
|---|---|---|
| PostgreSQL | ALTER TABLE old_name RENAME TO new_name | ALTER TABLE t RENAME COLUMN old_name TO new_name |
| SQLite (modern versions) | ALTER TABLE old_name RENAME TO new_name | ALTER TABLE t RENAME COLUMN old_name TO new_name |
| MySQL 8.0 | RENAME TABLE old_name TO new_name | ALTER TABLE t RENAME COLUMN old_name TO new_name |
| SQL Server | sp_rename with the object name | sp_rename with qualified column name and COLUMN object type |
Example MySQL syntax:
SQLRENAME TABLE products TO catalog_items; ALTER TABLE catalog_items RENAME COLUMN label TO item_name;
Example SQL Server syntax:
SQLEXEC sp_rename 'dbo.products', 'catalog_items'; EXEC sp_rename 'dbo.catalog_items.label', 'item_name', 'COLUMN';
The MySQL and SQL Server examples are illustrative, not tested in this session. Older MySQL versions may require CHANGE COLUMN and a full column definition, making accidental changes to attributes possible. SQL Server's sp_rename has its own limitations and does not automatically update every reference in dependent modules. Use current vendor documentation before deployment.
8. Transactions and rollback#
PostgreSQL and SQLite support transactional execution for these ordinary rename operations. A rename inside an uncommitted transaction can generally be rolled back. The following example was tested in SQLite after the first successful example, with the table already named catalog_items:
SQLBEGIN; ALTER TABLE catalog_items RENAME COLUMN item_name TO title; ROLLBACK;
After the rollback, inspecting the table showed that its column was still called item_name. The row data was not recopied. Other engines may implicitly commit DDL, so do not assume that the same transaction behavior applies everywhere.
Once a migration has committed, reversing the rename requires a new migration, and reversing a name alone may not undo application changes or other concurrent schema modifications. For a live PostgreSQL database, even a metadata-only operation can require an ACCESS EXCLUSIVE lock; check lock contention and deployment timing.
9. Safe renaming checklist#
Before renaming a table or column in a real system, confirm the source exists, the intended new name is available in the relevant namespace, and the executing role is authorized. Inventory dependencies, update SQL queries, ORM models, generated types, data pipelines and reports, and preserve grants and access-control behavior. Test migration and rollback in a disposable copy or staging environment, coordinate the deployment, and verify both metadata and application behavior afterward. A migration may need an explicit compatibility strategy if multiple application versions are running; that is a separate advanced topic.
Supabase's database migration documentation recommends tracking schema changes in version-controlled migrations, testing locally, and reviewing changes before deploying. That guidance informs the deployment checklist here, rather than serving as a direct reference for PostgreSQL rename syntax.
10. Interview-ready answer#
In PostgreSQL and modern SQLite, use ALTER TABLE old_table RENAME TO new_table to rename a table and ALTER TABLE table_name RENAME COLUMN old_column TO new_column to rename a column. These DDL statements change names without deleting the existing rows or changing column values. However, dependent application queries, ORM mappings and database objects must be reviewed, and syntax and transaction behavior vary by database engine. In production, use a versioned migration and verify both the renamed object and dependent application behavior.
Common follow-up questions#
- Does renaming a table delete its rows? No. The same table data remains under its new name.
- Does renaming a column change its data type? No. Changing the name and changing the type are distinct operations.
- Can I use a SELECT alias instead? An alias changes the name in a query result, not the stored schema.
- Can a rename break application code? Yes. Code that refers directly to obsolete identifiers may fail.
- Is rollback always possible? No. It depends on engine and transaction semantics, and whether the rename has already committed.
Validation and references#
Eight checks passed in an isolated in-memory SQLite 3.46.1 database: table rename preserved rows, column rename preserved rows, new metadata matched, obsolete table reference failed, obsolete column reference failed, foreign-key dependency remained valid, the view returned its original rows, and transaction rollback restored the column name. No educational DDL was executed against the publishing database. PostgreSQL-specific introspection, schema-qualified syntax, MySQL and SQL Server examples, and PostgreSQL lock behavior were not independently executed or benchmarked.
The Mermaid flowchart has the exact triple-backtick mermaid fence and actual line breaks; a restricted structural syntax check passed. Full Mermaid parser and public rendering verification were unavailable.
Documentation consulted through the Supabase documentation connector:
- Supabase — Database migrations: migration tracking and local testing.
- Supabase — Local development workflow: version-controlled schema changes and migration review.
Additional authoritative reading (not live-verified in this session):
- PostgreSQL — ALTER TABLE.
- SQLite — ALTER TABLE.
- MySQL — RENAME TABLE.
- MySQL — ALTER TABLE.
- Microsoft SQL Server — sp_rename.
Live web browsing was unavailable; additional vendor links are suggested reading, not independently consulted sources.
References
- 1.Supabase — Database migrationssupabase.com
- 2.Supabase — Local development workflowsupabase.com
- 3.PostgreSQL — ALTER TABLE (not live-verified)postgresql.org
- 4.SQLite — ALTER TABLE (not live-verified)sqlite.org
- 5.MySQL — RENAME TABLE (not live-verified)dev.mysql.com
- 6.MySQL — ALTER TABLE (not live-verified)dev.mysql.com
- 7.SQL Server — sp_rename (not live-verified)learn.microsoft.com
Related questions
Explore other topics:Forward Deployed EngineeringPythonAI System DesignMachine Learning