JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Rename columns in MySQL database

Author:JIYIK Last Updated:2025/04/25 Views:

In this article, we aim to explore different ways to rename columns in MySQL.

ALTER TABLEThe command is mainly used to change the format of a given MySQL table. It can be used to add columns, change the data type within a column, delete columns, and sometimes even rename the entire database.

ALTER TABLEThere are two main ways to change column names using the command.

  • Statements with ALTER TABLEcommandsRENAME
  • Statements with ALTER TABLEcommandsCHANGE

Before we get started, however, we need to create a dummy dataset to work with. Here, we create a table, student_details, and a few rows in it.

-- create the table student_details
CREATE TABLE student_details(
  stu_id int,
  stu_firstName varchar(255) DEFAULT NULL,
  stu_lastName varchar(255) DEFAULT NULL,
  primary key(stu_id)
);
-- insert rows to the table student_details
INSERT INTO student_details(stu_id,stu_firstName,stu_lastName) 
 VALUES(1,"Preet","Sanghavi"),
 (2,"Rich","John"),
 (3,"Veron","Brow"),
 (4,"Geo","Jos"),
 (5,"Hash","Shah"),
 (6,"Sachin","Parker"),
 (7,"David","Miller");

The above query creates a table and rows in it containing the first and last name of the students. To see the entries in the data, we use the following code.

SELECT * FROM student_details;

The above line of code will give the following output.

stu_id	stu_firstName	stu_lastName
1	      Preet	        Sanghavi
2	      Rich	        John
3	      Veron	        Brow
4	      Geo	        Jos
5	      Hash	        Shah
6	      Sachin	    Parker
7	      David	        Miller

Let's aim to stu_firstNamerename the columns to something simple firstName.

RENAMERename columns using the

One of the most basic ways to change the name of a column in MySQL is by using RENAME COLUMNthe -CLIENT clause. The syntax for performing the above operation can be stated as follows:

ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

Here, old_namerepresents the column name to be changed and new_namerepresents the new name of the column.

To change stu_firstNamethe column, we would use the following code:

ALTER TABLE student_details RENAME COLUMN stu_firstName TO firstName;

The above line of code will give the following output:

stu_id    firstName 	stu_lastName
1	      Preet	        Sanghavi
2	      Rich	        John
3	      Veron	        Brow
4	      Geo	        Jos
5	      Hash	        Shah
6	      Sachin	    Parker
7	      David	        Miller

CHANGERename columns using the

A more complex way to change the name of a column is to use CHANGEthe clause. This clause also helps to adjust the data type of the column as well as the column name. The syntax to do this is as follows:

ALTER TABLE table_name CHANGE old_name new_name Data Type;

To stu_firstNamechange the column and its data type to VARCHAR(40), we will use the following code:

ALTER TABLE student_details CHANGE stu_firstName firstName VARCHAR(40);
-- Here VARCHAR(40) is the new data type

The above code snippet will give the following output using the new data types mentioned above.

stu_id    firstName 	stu_lastName
1	      Preet	        Sanghavi
2	      Rich	        John
3	      Veron	        Brow
4	      Geo	        Jos
5	      Hash	        Shah
6	      Sachin	    Parker
7	      David	        Miller

So, with the help of the above two techniques, we can effectively change the name of any column of a table in MySQL. We can conclude that understanding ALTER TABLEthe command is a prerequisite for exploring complex statements.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to select from multiple tables in MySQL

Publish Date:2025/04/25 Views:57 Category:MySQL

This article explains how to use MySQL to query from multiple tables in one script SELECT . Let's demonstrate a situation: SELECT name , price, details, type , FROM food, food_order WHERE breakfast.id = 'breakfast_id' Now, let's imagine FRO

Creating a table from CSV in MySQL

Publish Date:2025/04/25 Views:114 Category:MySQL

In this article, we aim to understand how to create a table from CSV in MySQL database. Businesses and organizations must quickly generate tables from large amounts of data. These organizations typically have large CSV files with large amou

Creating a Temporary Table in MySQL

Publish Date:2025/04/25 Views:183 Category:MySQL

In this article, we aim to explore different ways to create temporary tables in MySQL. One of the main features of temporary tables is that it helps in storing temporary data. This feature is enabled in MySQL 3.23 and later versions. These

Truncate all tables in Mysql

Publish Date:2025/04/25 Views:89 Category:MySQL

Today I will show you how to truncate all tables in Mysql. It is used when you want to delete the entire table TRUNCATE TABLE . TRUNCATE It is a type of DML statement, which means it cannot be rolled back once it is committed. There are two

Different ways to check if a row exists in a MySQL table

Publish Date:2025/04/25 Views:163 Category:MySQL

This article highlights different ways to check if a row exists in a MySQL table. We will use the EXISTS and NOT EXISTS operators. We can also use these two operators with IF() the function to get a meaningful message if a row (a record) is

Check if table exists in MySQL

Publish Date:2025/04/25 Views:194 Category:MySQL

This article provides several options to check if a table exists in MySQL. Before discussing it, let us first see what a table is in MySQL and when you need to check its existence. What is a table in MySQL? A table is a database object that

Get column names in MySQL

Publish Date:2025/04/25 Views:109 Category:MySQL

In this article, we aim to explore how to get the column names of a specific table in MySQL database. Often, when working with data in MySQL, we tend to forget the column names of a particular table in the database and the data types of dif

Get the version in MySQL

Publish Date:2025/04/24 Views:170 Category:MySQL

In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T

Full Join in MySQL

Publish Date:2025/04/24 Views:71 Category:MySQL

This article aims to explore how to perform a full join or full outer join in MySQL. Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_id and student_

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial