Sub topic 7
Renaming Columns and Tables
Prerequisites and the effect of renaming
This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE statements, can create tables, and understand primary keys and foreign keys.
A rename changes an object's identifier, such as a table name or column name. It normally keeps the stored rows, data type, constraints, and indexes unchanged. However, SQL statements that still use the old name will fail.
For example, changing customers.name to customers.full_name requires application queries, reports, views, and procedures to use full_name as well. Some database systems update dependent objects automatically, while others leave dependencies broken or handle them differently. Check the behavior of your database before renaming a production object.
Renaming a column
The portable-looking syntax supported by PostgreSQL, MySQL 8.0 and later, Oracle, and SQLite 3.25 and later is:
ALTER TABLE customers
RENAME COLUMN name TO full_name;This statement changes only the column's name. It does not change the column's type or its values. For example, a VARCHAR(100) NOT NULL column remains VARCHAR(100) NOT NULL.
SQL Server uses sp_rename instead:
EXEC sp_rename
'dbo.customers.name',
'full_name',
'COLUMN';The exact object name may include a schema. Use the schema explicitly when the database contains more than one object with the same name.
Renaming a table
PostgreSQL, MySQL, and SQLite use this form:
ALTER TABLE customers
RENAME TO clients;Oracle also supports:
RENAME customers TO clients;SQL Server uses sp_rename:
EXEC sp_rename
'dbo.customers',
'clients';After the rename, this query uses the new table name:
SELECT id, full_name
FROM clients;A table rename can affect foreign-key definitions, views, triggers, stored procedures, ORM mappings, scheduled jobs, and permissions. The database may preserve some dependencies, but client code still needs to be checked.
Safe rename workflow
- Search the schema and application code for the old name.
- Check views, triggers, procedures, permissions, and foreign keys.
- Apply the rename in a test database.
- Update dependent code in a coordinated deployment.
- Run queries and integration tests before removing compatibility support.
A rename is often quick, but it can cause a wide outage when old and new application versions run at the same time.
Exercise
Does renaming customers.name to full_name change the column's data type or existing values?
Show answer
No. It changes the identifier only, although queries and other objects that use name may need updates.