Need to rename a column in SQL? Whether you’re tidying up your database or making names more intuitive, this guide has you covered. Each database—PostgreSQL, MySQL, SQL Server, and Oracle—has its own way of doing it, and I’ll walk you through them step-by-step with examples. Let’s get started!
Table of Contents
- Why Rename a Column?
- Quick Reference Table
- How to Rename a Column in Different Databases
- Tips to Keep Things Smooth
- Wrapping It Up
Why Rename a Column?
Renaming a column can make your database clearer—like changing emp_id
to employee_id
for better readability. The best part? It only updates the name, not the data. However, the syntax varies by database, so let’s dive into the specifics.
Let’s look at a practical example using this database schema:
This diagram shows a simple employee management system with the following structure:
- Tables:
employees
- Columns:
emp_id
(to be renamed toemployee_id
)first_name
last_name
hire_date
department_id
(foreign key)
- Columns:
departments
Quick Reference Table
Database | Syntax | Auto-updates Dependencies? | Version Notes |
---|---|---|---|
PostgreSQL | ALTER TABLE table RENAME COLUMN old_name TO new_name; | Yes | All versions |
MySQL 8.0+ | ALTER TABLE table RENAME COLUMN old_name TO new_name; | No | 8.0+ only |
MySQL 5.6 | ALTER TABLE table CHANGE old_name new_name datatype; | No | 5.6 and below |
SQL Server | EXEC sp_rename 'table.old_name', 'new_name', 'COLUMN'; | No | All versions |
Oracle | ALTER TABLE table RENAME COLUMN old_name TO new_name; | Yes | All versions |
How to Rename a Column in Different Databases
Here’s how to rename a column in each system, with examples and key notes.
PostgreSQL
- Syntax: Use
ALTER TABLE
—it’s simple and clean. - Example: Rename
emp_id
toemployee_id
in anemployees
table:ALTER TABLE employees RENAME COLUMN emp_id TO employee_id;
- Notes:
- PostgreSQL automatically updates dependent objects like views or constraints.
- Just ensure the new name is unique and you have
ALTER
privileges.
MySQL
- Syntax: Depends on your version:
- Older versions (5.6 and below): Include the data type:
ALTER TABLE employees CHANGE emp_id employee_id INT;
- Newer versions (8.0 and up): Simpler syntax:
ALTER TABLE employees RENAME COLUMN emp_id TO employee_id;
- Older versions (5.6 and below): Include the data type:
- Example: For MySQL 8.0+, use the second command to rename
emp_id
toemployee_id
. - Notes:
- In older versions, don’t forget the data type.
- You may need to manually update views or triggers.
SQL Server
- Syntax: Use the
sp_rename
stored procedure. - Example: Rename
emp_id
toemployee_id
in theemployees
table:EXEC sp_rename 'employees.emp_id', 'employee_id', 'COLUMN';
- Notes:
- Include
'COLUMN'
to specify it’s a column rename. - Dependent objects (e.g., views) won’t update automatically—adjust them manually.
- Include
Oracle
- Syntax: Another
ALTER TABLE
approach. - Example: Rename
emp_id
toemployee_id
:ALTER TABLE employees RENAME COLUMN emp_id TO employee_id;
- Notes:
- Oracle updates dependent objects for you.
- Ensure the new name is unique and you have the right privileges.
Tips to Keep Things Smooth
Renaming columns is straightforward, but these tips can save you trouble:
- Backup your database: Always have a safety net before making changes.
- Verify permissions: You’ll typically need
ALTER
privileges. - Check dependencies: SQL Server and MySQL require manual updates to views or triggers; PostgreSQL and Oracle handle this automatically.
- Test first: Run the command in a test environment if possible.
- Ensure unique names: The new column name can’t already exist in the table.
Wrapping It Up
Now you’re ready to rename columns like a pro in PostgreSQL, MySQL, SQL Server, or Oracle! Each system has its quirks, but with these examples and tips, you’ve got it under control. Back up your data, double-check your syntax, and you’ll be set. Happy querying!
Explore, learn, and share SQL queries in our free online SQL playground. Support for MySQL, PostgreSQL, and SQL Server, perfect for testing and enhancing your SQL skills.
Supports: