How to change column names in mysql?

Member

by lottie , in category: MySQL , 6 months ago

How to change column names in mysql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 6 months ago

@lottie 

To change column names in MySQL, you can use the ALTER TABLE statement along with the CHANGE keyword. Here's the syntax:

1
ALTER TABLE table_name CHANGE current_column_name new_column_name column_definition;


Example:


Let's say we have a table called employees with the following structure:

1
2
3
4
5
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  age INT
);


Now, let's change the name of the 'age' column to 'years_old':

1
ALTER TABLE employees CHANGE age years_old INT;


This will change the column name from 'age' to 'years_old' while keeping the data type of the column as INT.