How to rename a column in SQL - Guide for PostgreSQL, MySQL, SQL Server, and Oracle

Renaming a column in SQL is a common task for database management, but the process differs across databases like PostgreSQL, MySQL, SQL Server, and Oracle. This guide will walk you through how to do it for each, using clear examples and addressing potential challenges.
How to rename a column in SQL - Guide for PostgreSQL, MySQL, SQL Server, and Oracle

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?

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 to employee_id)
        • first_name
        • last_name
        • hire_date
        • department_id (foreign key)
    • departments

Quick Reference Table

DatabaseSyntaxAuto-updates Dependencies?Version Notes
PostgreSQLALTER TABLE table RENAME COLUMN old_name TO new_name;YesAll versions
MySQL 8.0+ALTER TABLE table RENAME COLUMN old_name TO new_name;No8.0+ only
MySQL 5.6ALTER TABLE table CHANGE old_name new_name datatype;No5.6 and below
SQL ServerEXEC sp_rename 'table.old_name', 'new_name', 'COLUMN';NoAll versions
OracleALTER TABLE table RENAME COLUMN old_name TO new_name;YesAll 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 to employee_id in an employees 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;
  • Example: For MySQL 8.0+, use the second command to rename emp_id to employee_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 to employee_id in the employees 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.

Oracle

  • Syntax: Another ALTER TABLE approach.
  • Example: Rename emp_id to employee_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!

RunSQL - Online SQL Playground for MySQL, PostgreSQL, and SQL Server

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:

MySQL SQL Server PostgreSQL
Try RunSQL Today