Change Table Name In MySQL
In this tutorial, we'll learn how to change the name of a table in MySQL.
Syntax
The basic syntax to change the name of a table in MySQL is as follows:
ALTER TABLE current_table_name RENAME TO new_table_name;
Here, current_table_name
is the name of the table you want to rename, and new_table_name
is the new name you want to give to the table.
Example
Suppose we have a table called students
and we want to rename it to class_students
. Here's how we can do it using the ALTER TABLE
statement:
ALTER TABLE students RENAME TO class_students;
Output
If the table rename operation is successful, MySQL will return a message like this:
Query OK, 0 rows affected (0.03 sec)
If there is an error, MySQL will return an error message with details about the problem.
Explanation
In the example above, we used the ALTER TABLE
statement to change the name of the students
table to class_students
. The RENAME TO
clause is used to specify the new name of the table.
Use
Changing the name of a table in MySQL is a common operation when you want to give a more meaningful or descriptive name to a table, or when you want to follow a naming convention. It is also useful when you want to merge two tables together, or when you want to split a large table into smaller ones.
Important Points
- The name of the table must be unique in the database.
- Changing the name of a table will not affect the data contained in the table.
- Dropping a table with a new name that already exists before renaming the original table will result in an error.
Summary
In this tutorial, we learned how to change the name of a table in MySQL using the ALTER TABLE
statement. We covered the syntax, example, output, explanation, use, and important points of changing the name of a table in MySQL. By following these instructions, you can easily rename tables in your MySQL database to better reflect their contents and adhere to naming conventions.