Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename a column in MySQL

Tags:

sql

mysql

I am trying to rename a column in MySQL community server 5.5.27 using this SQL expression:

ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name; 

I also tried

ALTER TABLE table_name RENAME old_col_name TO new_col_name; 

But it says:

Error: check the Manual that corresponds to your MySQL server version

like image 980
Michael Okoli Avatar asked May 17 '15 18:05

Michael Okoli


People also ask

How do I edit a column in MySQL?

The syntax to modify a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name MODIFY column_name column_definition [ FIRST | AFTER column_name ]; table_name. The name of the table to modify.

Can we rename a column?

You can rename a column with the below code. You select the table with ALTER TABLE table_name and then write which column to rename and what to rename it to with RENAME COLUMN old_name TO new_name .

How do I rename a column in MariaDB?

Using MariaDB alter table to rename a column in a table. In this syntax: First, specify the name of the table from which you want to rename the column after the alter table keywords. Second, specify the name of the column and the new name followed by the column definition after the change column keywords.


2 Answers

Use the following query:

ALTER TABLE tableName CHANGE oldcolname newcolname datatype(length); 

The RENAME function is used in Oracle databases.

ALTER TABLE tableName RENAME COLUMN oldcolname TO newcolname datatype(length); 

@lad2025 mentions it below, but I thought it'd be nice to add what he said. Thank you @lad2025!

You can use the RENAME COLUMN in MySQL 8.0 to rename any column you need renamed.

ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name; 

ALTER TABLE Syntax: RENAME COLUMN:

  • Can change a column name but not its definition.
  • More convenient than CHANGE to rename a column without changing its definition.
like image 186
Rizky Fakkel Avatar answered Oct 04 '22 07:10

Rizky Fakkel


In Server version: 5.6.34 MySQL Community Server

ALTER TABLE table_name CHANGE COLUMN old_column_name new_column_name data_type; 
like image 43
Kanke Avatar answered Oct 04 '22 09:10

Kanke