00:00

MySQL Alter Table - Rename Column

Last updated: 4 min read

Short or cryptic column names like cust_nm, ph and amt make SQL hard to read. MySQL renames columns with ALTER TABLE … RENAME COLUMN (MySQL 8.0 and later) or with the older CHANGE COLUMN. The data stays exactly as it is; only the name changes.

1. Syntax

syntax.sql
-- MySQL 8.0+
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

-- any version (must repeat the full column definition)
ALTER TABLE table_name CHANGE [COLUMN] old_name new_name column_definition;
RENAME COLUMN versus CHANGE COLUMNRENAME COLUMN cust_nm TO customer_name changes only the name and keeps the type and attributes. CHANGE COLUMN needs the full definition repeated; leaving out NOT NULL or DEFAULT removes them.RENAME COLUMN (8.0+)RENAME COLUMN cust_nmTO customer_namename only, definition kept ✔CHANGE COLUMNCHANGE cust_nm customer_nameVARCHAR(60) NOT NULLmust restate the full definition(forget NOT NULL → it is lost)
Figure 1: prefer RENAME COLUMN; it can't accidentally change the column's type or attributes.

2. Renaming with RENAME COLUMN

rename.sql
ALTER TABLE customers RENAME COLUMN cust_nm TO customer_name;
SHOW FULL COLUMNS FROM customers;

Result: 3 rows

FieldTypeNullKeyDefaultComment
cust_idintNOPRINULL
customer_namevarchar(60)NOMULunknownfull name
phvarchar(15)YESNULL

The type, NOT NULL, default, comment and the index all came along unchanged.

3. Renaming with CHANGE COLUMN

CHANGE needs the full definition. Watch what happens if you give only the type:

change-careless.sql
CREATE TABLE demo_change LIKE customers;
ALTER TABLE demo_change CHANGE customer_name full_name VARCHAR(60);
SHOW FULL COLUMNS FROM demo_change LIKE 'full_name';

Result: 1 row

FieldTypeNullDefaultComment
full_namevarchar(60)YESNULL

NOT NULL, the default and the comment are gone. Restating everything keeps them:

change-careful.sql
ALTER TABLE customers CHANGE ph phone VARCHAR(15) NULL COMMENT 'mobile number';
SHOW FULL COLUMNS FROM customers LIKE 'phone';

Result: 1 row

FieldTypeNullDefaultComment
phonevarchar(15)YESNULLmobile number
Use CHANGE when you want to rename and change the type in one step; otherwise use RENAME COLUMN.

4. Renaming Several Columns at Once

several.sql
ALTER TABLE orders
  RENAME COLUMN ord_id TO order_id,
  RENAME COLUMN amt    TO amount;
SELECT * FROM orders;

Result: 3 rows

order_idcust_idamount
101450.00
1121200.00
12199.00

5. What Happens to Related Objects

ObjectAfter renaming a column
DataUnchanged
Indexes on the columnUpdated automatically
Foreign keys (in this table or pointing to it)Updated automatically
CHECK constraints and generated columnsUpdated automatically
ViewsNot updated: they break
Stored procedures, triggers, application SQLNot updated: fix them yourself

The primary key on order_id followed the rename automatically, but the view still refers to ord_id and amt:

query.sql
SELECT * FROM big_orders;
Output
ERROR 1356 (HY000): View 'sales.big_orders' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
fix-view.sql
CREATE OR REPLACE VIEW big_orders AS SELECT order_id, amount FROM orders WHERE amount > 400;
SELECT * FROM big_orders;

Result: 2 rows

order_idamount
10450.00
111200.00

6. Common Errors

query.sql
ALTER TABLE customers RENAME COLUMN customer_name TO phone;
Output
ERROR 1060 (42S21): Duplicate column name 'phone'
query.sql
ALTER TABLE customers RENAME COLUMN cust_nm TO name;
Output
ERROR 1054 (42S22): Unknown column 'cust_nm' in 'customers'
ErrorCause
1060 Duplicate column nameThe new name is already used in the table
1054 Unknown columnThe old name is wrong or was already renamed
1064 syntax error near 'COLUMN'RENAME COLUMN used on MySQL 5.7 or older; use CHANGE

7. Renaming Safely in a Live Application

  1. Search the application code, reports and stored programs for the old name.
  2. Deploy code that works with both names if possible (for example through a view), or schedule a short maintenance window.
  3. Rename the column; it's an instant metadata change for InnoDB.
  4. Recreate views and update routines and triggers.
  5. With JPA/Hibernate, update @Column(name = "…") mappings.

8. Practice Exercises

Exercise 1: Rename orders.cust_id to customer_id and check the foreign key still works.
solution.sql
ALTER TABLE orders RENAME COLUMN cust_id TO customer_id;
SELECT constraint_name, column_name, referenced_column_name
FROM information_schema.key_column_usage
WHERE table_schema = 'sales' AND table_name = 'orders' AND referenced_table_name IS NOT NULL;

Result: 1 row

CONSTRAINT_NAMECOLUMN_NAMEREFERENCED_COLUMN_NAME
fk_ord_custcustomer_idcust_id
Exercise 2: Rename customers.phone to mobile and make it VARCHAR(20) in one statement.
solution.sql
ALTER TABLE customers CHANGE phone mobile VARCHAR(20) NULL COMMENT 'mobile number';
SHOW COLUMNS FROM customers LIKE 'mobile';

Result: 1 row

FieldTypeNullKeyDefaultExtra
mobilevarchar(20)YESNULL

9. Summary

  • ALTER TABLE t RENAME COLUMN old TO new; (8.0+) renames and keeps the definition.
  • CHANGE old new definition renames and redefines; restate every attribute you want to keep.
  • Rename several columns in one ALTER TABLE.
  • Indexes, foreign keys and checks follow; views, routines and application code don't.