The basic command to rename a column depends on which database system you use
SQL does not have a single standard command for renaming columns across all database systems. Instead, each major database — MySQL, PostgreSQL, SQL Server, and Oracle — has its own syntax. The most common approach is the ALTER TABLE statement, but the exact keywords that follow it change depending on your system.
Before you rename anything, back up your database or at least the table you are working on. A rename is permanent and affects every query, report, and process that references that column by name.
Key Takeaways
- MySQL uses ALTER TABLE tablename CHANGE oldname newname datatype, and you must include the column's data type even if you are not changing it.
- PostgreSQL uses ALTER TABLE tablename RENAME COLUMN oldname TO newname, which is the simplest syntax of the major systems.
- SQL Server uses EXEC sp_rename 'tablename.oldname', 'newname', a stored procedure rather than a standard ALTER statement.
- Oracle uses ALTER TABLE tablename RENAME COLUMN oldname TO newname, similar to PostgreSQL.
- Always test the rename on a copy of your table first, because the change affects every process and report that uses that column name.
MySQL: CHANGE requires you to repeat the data type
In MySQL, the command is ALTER TABLE CHANGE. You must specify the column's data type even if you are not changing it, which trips up many people on their first try.
The syntax is:
ALTER TABLE tablename CHANGE oldcolumnname newcolumnname datatype;
For example, if you have a table called customers with a column called phone that is a VARCHAR(15), and you want to rename it to phone_number, you would write:
ALTER TABLE customers CHANGE phone phone_number VARCHAR(15);
If you forget to include the data type, MySQL will return an error. If you want to change the data type at the same time as renaming, you can do that in the same command — just replace datatype with the new type.
PostgreSQL and Oracle: RENAME COLUMN is straightforward
PostgreSQL and Oracle both use the same, simpler syntax: ALTER TABLE RENAME COLUMN. You do not need to specify the data type.
The syntax is:
ALTER TABLE tablename RENAME COLUMN oldcolumnname TO newcolumnname;
For example, in PostgreSQL or Oracle, to rename phone to phone_number in the customers table:
ALTER TABLE customers RENAME COLUMN phone TO phone_number;
This is the most readable of the three approaches and requires the fewest details. If you are learning SQL and have a choice of database system, PostgreSQL's syntax is the easiest to remember.
SQL Server: sp_rename is a stored procedure, not an ALTER statement
SQL Server does not use ALTER TABLE for column renames. Instead, it uses a built-in stored procedure called sp_rename. The syntax is different from the other systems and can feel less intuitive at first.
The syntax is:
EXEC sp_rename 'tablename.columnname', 'newcolumnname';
To rename phone to phone_number in the customers table in SQL Server:
EXEC sp_rename 'customers.phone', 'phone_number';
Notice that the table and column are joined with a dot inside a single quoted string, and the new name is a separate parameter. SQL Server will return a warning that says "Caution: Changing any part of an object name could break scripts and stored procedures." This is just a warning — the rename will still complete.
What happens to dependent objects after a rename
When you rename a column, any view, stored procedure, or trigger that references that column by name will break. SQL Server will warn you about this; PostgreSQL and MySQL will not, but the problem exists in all systems.
Before you rename a column in a production database, search your codebase and documentation for every place that column name appears. This includes SELECT statements, WHERE clauses, JOIN conditions, and any process code that builds queries. If you rename without updating these references, your queries will fail with "column not found" errors.
If you are working in a small database or a test environment, the risk is lower. If you are working on a live system with many applications or reports depending on it, consider whether the rename is worth the work of updating everything downstream.
Testing the rename on a copy first
The safest approach is to make a copy of your table, rename the column in the copy, and run your queries against it to make sure everything still works. Only then should you rename the column in the real table.
To create a copy of a table in most systems, you can use CREATE TABLE AS SELECT. For example, in PostgreSQL:
CREATE TABLE customers_copy AS SELECT * FROM customers;
Then rename the column in the copy, test your process or reports, and once you are confident, run the same rename command on the original table. This approach takes a few extra minutes but saves you from discovering problems after the rename is live.
Frequently Asked Questions
Can I rename a column that is part of a primary key or foreign key?
Yes, you can rename the column itself. However, the constraint name stays the same unless you also rename the constraint separately. Some systems will automatically update the constraint to reference the new column name; others will not. Test this on a copy first, because the behavior varies by database system and version.
What if I rename a column and then realize I made a mistake?
You can rename it back using the same command with the old and new names reversed. There is no undo function in SQL, so if you did not back up the database first, you will have to rename it manually. This is why a backup before any structural change is important.
Do I need special permissions to rename a column?
Yes. You need ALTER permission on the table. In most systems, only the table owner or a database administrator can rename columns. If you try to rename a table you do not own, you will get a permission denied error.
Can I rename multiple columns at once?
No. Each database system requires you to rename columns one at a time using separate ALTER TABLE statements. If you need to rename many columns, you will write multiple commands, but you can run them all in sequence in a single script.
Will renaming a column affect the data stored in it?
No. The rename changes only the column name, not the data inside it. All the values stay exactly as they were. Only the name that you use to reference the column in queries changes.