The basic command to rename a column
The way you rename a column in SQL depends on which database system you use. MySQL, PostgreSQL, SQL Server, and Oracle each have different syntax. The most common approach across systems is the ALTER TABLE statement, but the exact keywords differ.
In MySQL, you use ALTER TABLE with CHANGE or RENAME COLUMN. In PostgreSQL, you use ALTER TABLE with RENAME COLUMN. In SQL Server, you use sp_rename as a stored procedure. In Oracle, you use ALTER TABLE with RENAME COLUMN. If you work with multiple databases, knowing which one you are connected to before you run the command will save you from syntax errors.
Key Takeaways
- MySQL uses ALTER TABLE table_name CHANGE old_name new_name datatype, or ALTER TABLE table_name RENAME COLUMN old_name TO new_name in version 8.0 and later.
- PostgreSQL uses ALTER TABLE table_name RENAME COLUMN old_name TO new_name.
- SQL Server uses EXEC sp_rename 'table_name.old_name', 'new_name', 'COLUMN'.
- Oracle uses ALTER TABLE table_name RENAME COLUMN old_name TO new_name.
- Always back up your database or test the command on a copy before running it on a live table.
MySQL syntax for renaming a column
In MySQL 8.0 and later, the simplest command is:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
If you are using an older version of MySQL (before 8.0), use the CHANGE keyword instead. With CHANGE, you must also specify the column's data type:
ALTER TABLE table_name CHANGE old_column_name new_column_name VARCHAR(255);
Replace VARCHAR(255) with whatever data type the column actually holds — INT, DATE, TEXT, DECIMAL, and so on. If you get the data type wrong, the column will change type, which can corrupt your data. To be safe, run a SELECT statement first to see the column definition, or check your database schema.
PostgreSQL syntax for renaming a column
PostgreSQL uses a straightforward ALTER TABLE command:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
PostgreSQL does not require you to specify the data type because it only changes the name, not the structure. This makes it less error-prone than MySQL's CHANGE syntax. If the column is referenced by indexes, constraints, or views, PostgreSQL will update those references automatically in most cases, though you should test this on a non-production copy first.
SQL Server syntax for renaming a column
SQL Server uses a stored procedure called sp_rename instead of an ALTER TABLE statement:
EXEC sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN';
The third parameter, 'COLUMN', tells SQL Server that you are renaming a column and not a table or index. Without it, the command will fail. SQL Server also updates dependent objects like views and stored procedures that reference the old column name, though you should verify this in your own environment before running it on production data.
Oracle syntax for renaming a column
Oracle uses ALTER TABLE with RENAME COLUMN:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
Oracle's syntax is similar to PostgreSQL and does not require you to specify the data type. If the column is part of a primary key, foreign key, or index, Oracle will handle the rename, but dependent code in stored procedures or views will not update automatically — you will need to modify those separately.
What happens to data and dependent objects
Renaming a column does not change the data itself, only the name. All the values in that column stay exactly as they are. However, anything that references the old column name — such as views, stored procedures, triggers, or process code — will break unless you update it.
Some databases like PostgreSQL and SQL Server will update views and some dependent objects automatically. Others, like Oracle, will not. Before you rename a column in a production database, search your codebase and database for references to the old name. A straightforward find-and-replace in your process code is usually faster and safer than relying on the database to update everything for you.
Testing the rename before running it on live data
The safest approach is to test on a copy of your database first. Create a backup, restore it to a test environment, and run the rename command there. Check that your process still works, that reports still run, and that any code that queries that column still functions.
If you cannot create a test copy, at least run a SELECT statement on the table to confirm the old column name exists and holds the data you expect. Then run the rename in a transaction so you can roll it back if something goes wrong. In MySQL, PostgreSQL, and SQL Server, you can use BEGIN and ROLLBACK to undo the change if needed — though Oracle's DDL statements (like ALTER TABLE) cannot be rolled back in the same way.
Common mistakes to avoid
The most common error is using the wrong syntax for your database system. If you copy a command from a tutorial written for MySQL and run it in SQL Server, it will fail. Always check which database you are connected to before you execute the command.
Another mistake is forgetting to update process code that references the old column name. The rename will succeed, but your process will throw errors when it tries to query a column that no longer exists. Search your code for the old column name before you rename, and update it everywhere it appears.
In MySQL, using CHANGE instead of RENAME COLUMN and getting the data type wrong will silently change the column's type, which can truncate or corrupt data. If you must use CHANGE, copy the exact data type from your schema first.
Frequently Asked Questions
Can I rename a column that is part of a primary key or foreign key?
Yes, but the database will update the constraint definition automatically in most cases. PostgreSQL and SQL Server handle this well. Oracle will rename the column but may not update the constraint name itself — you may need to drop and recreate the constraint. Always test this on a copy first.
What if I rename a column and my process breaks?
Roll back the change if possible (in MySQL, PostgreSQL, and SQL Server, you can use ROLLBACK if you ran the command in a transaction). Then update your process code to use the new column name and test it before running the rename again. If you cannot roll back, you will need to rename the column back to its original name and fix your code first.
Do I need to rebuild indexes after renaming a column?
No. Indexes are tied to the column by its internal ID, not its name, so they continue to work after a rename. However, if you have code or queries that reference the index by name, you may need to update those.
Can I rename a column in a view?
No. Views are read-only definitions based on underlying tables. To rename a column in a view, you must drop the view, rename the column in the base table, and recreate the view with the new column name.
What is the difference between CHANGE and RENAME COLUMN in MySQL?
CHANGE is the older syntax and requires you to specify the data type, which can cause accidental type changes if you get it wrong. RENAME COLUMN (available in MySQL 8.0+) only changes the name and is safer. Use RENAME COLUMN if your MySQL version supports it.