Renaming a table in MySQL
This article will explain how to rename a table in a MySQL database.
Often, an organization's product requirements change, and as a result, the names of tables and columns in a particular database need to be constantly changed. These changes will help reflect the updated information. MySQL uses several techniques to help us do this efficiently.
To change the name of a specific table, we use RENAME TABLE
the statement as shown below.
RENAME TABLE old_table_name TO new_table_name;
We can rename tables in MySQL using the following two techniques.
-
Usage
RENAME TABLE
method. -
Usage
ALTER TABLE
method.
Before we get started, let's create a dummy dataset to work with. Here, we create a table student_details
and a few rows.
-- 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");
Now let's student_details
rename the table to student_information
table.
RENAME
Rename a table
in MySQL using
We can rename
do this using the statement. This statement can be written as RENAME previous_table_name to new_name_to_be_assigned;
in MySQL. To student_details
perform the above operation on the table, we can use the following query.
RENAME TABLE student_details TO student_information;
Now, let's check student_information
the table using the following query.
SELECT * from student_information;
The above mentioned query will give us 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
It shows that the table has been renamed successfully and there is no tussle in the table data.
ALTER TABLE
Rename a table
in MySQL using
We can also use the RENAME statement in MySQL ALTER TABLE
to rename the table. The syntax for this operation is as follows.
ALTER TABLE previous_table_name RENAME new_name_to_be_assigned;
ALTER TABLE
In order to rename the table
using technique student_details
, we can use the following query.
ALTER TABLE student_details RENAME student_information;
Now, let's check student_information
the table using the following query.
SELECT * from student_information;
The above mentioned query will give us 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
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.
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
Rename columns in MySQL database
Publish Date:2025/04/25 Views:80 Category:MySQL
-
In this article, we aim to explore different ways to rename columns in MySQL. ALTER TABLE The 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 co
Copying a table in MySQL
Publish Date:2025/04/25 Views:142 Category:MySQL
-
The purpose of this article is to explore different ways to create a copy of a table in MySQL. The source table is also called the table to be copied, and the target table is called the clone table, which can be from the same or a different
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