

It can be used to rename a column and change the data type of that column with the same command.Įnter the following command in your MySQL client shell to change the name of the column and its definition: ALTER TABLE your_table_name CHANGE original_column_name new_col_name data_type The CHANGE clause offers important additions to the renaming process. If you need additional functions, such as changing the data definition, or position of a column, you need to use the CHANGE clause instead. The RENAME COLUMN clause can only be used to rename a column. ALTER TABLE RENAME is the existing syntax to rename the entire table. Note: The word COLUMN is obligatory for the ALTER TABLE RENAME COLUMN command. Keep in mind that you cannot rename a column to a name that already exists in the table. To change a column name, enter the following statement in your MySQL shell: ALTER TABLE your_table_name RENAME COLUMN original_column_name TO new_column_name Įxchange the your_table_name, original_column_name, and new_column_name with your table and column names. This clause is available since MySQL version 8.0. The simplest way to rename a column is to use the ALTER TABLE command with the RENAME COLUMN clause.
Mysql rename column how to#
How to Use the RENAME COLUMN Clause (MySQL 8.0) The commands are straightforward, and you may use the clause that fits your requirements best.

The difference is that the CHANGE clause can also be used to alter the data types of a column. The RENAME COLUMN and CHANGE clause both allow for the names of existing columns to be altered. The function that concerns us the most is how to utilize ALTER TABLE to rename a column.Ĭlauses give us additional control over the renaming process. You can use it to add or delete columns, change the type of data within the columns, and even rename entire databases. Good Luck!!!.Rename MySQL Column with ALTER TABLE CommandĪLTER TABLE is an essential command used to change the structure of a MySQL table. We hope this article helped you with the renaming of columns in a MySQL table. Output in image_5 shows that the column name is changed to annual_fee_submitted, and data got preserved as well. Let us see the changes made to the table by executing: SELECT * FROM student_enroll_data ALTER TABLE student_enroll_data RENAME COLUMN fee_submitted TO annual_fee_submitted We are renaming the column from fee_submitted to annual_fee_submitted. # see what got inserted into the table student_enroll_data INSERT INTO student_enroll_data(student_id,student_name,enroll_date,student_ssn_no,fee_submitted) Step2: Rename the column fee_submitted.Step1: We will insert some data to table student_enroll_data.Let us verify the same by executing the below queries. Using the RENAME clause column will not lose the existing data of the column. This section will see how to change the column name without losing the data already present in the column. MySQL change column name without losing the data Output in image_3 shows that the column’s name is changed back to fee_submitted from annual_fee_submitted. ALTER TABLE student_enroll_data CHANGE annual_fee_submitted fee_submitted DECIMAL(10,2) In the below query, we are changing the name of column annual_fee_submitted back to fee_submitted. Syntax of CHANGE in MySQL ALTER TABLE tableName CHANGE COLUMN oldColumnName newColumnName datatype Example of CHANGE in MySQL Output in image_2 shows that the column’s name is changed to annual_fee_submitted. In the below query, we are renaming the column fee_submitted to annual_fee_submitted in table student_enroll_data. Syntax of RENAME in MySQL ALTER TABLE tableName RENAME COLUMN oldColumnName TO newColumnName Example of RENAME in MySQL In this section, we will use RENAME and CHANGE clauses with ALTER statement to rename the column.
