Open In App

MySQL | RENAME USER

Last Updated : 10 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In MySQL, the RENAME USER statement provides a straightforward way to change the username of an existing user account without altering any associated privileges or settings. In this article, we will learn RENAME USER in MySQL in a step-by-step approach.

MySQL RENAME USER

  • In MySQL, the RENAME USER statement is used to change the username of an existing MySQL user account.
  • This command allows us to modify the username while keeping the same user privileges and settings.

Syntax:

RENAME USER 'username'@'host' TO 'new_username'@'host';

Parameters Used:

  1. Username: It is the username of the user account you want to rename.
  2. new_username: It is the new name you want to assign to the user.

Examples of MySQL RENAME USER

Suppose there are 4 users in the MySQL database server as listed below:

Let us now learn about how to rename users using RENAME USER statement:

Example 1: Renaming a single user using the RENAME USER statement

To rename the user account with the username "gfguser1", the RENAME USER statement should be executed as follows:

Syntax:

RENAME USER 'gfguser1 '@'localhost' TO 'Amit '@'localhost';

Output:

Example 2: Renaming multiple users using the RENAME USER statement

The RENAME USER statement can be used to rename multiple user accounts at once. To rename two user account "gfguser2" and "gfguser1" from the table mentioned above,the RENAME USER statement should be executed as follows:

Query:

RENAME USER
'gfguser1 '@'localhost' TO 'Raj'@'localhost',
'gfguser2 '@'localhost' TO 'Priya' @'localhost';

Output:

Conclusion

The RENAME USER statement in MySQL allows for efficient management of user accounts by enabling administrators to rename users easily. Whether updating a single user or multiple users at once, this command ensures that the new usernames take effect while retaining the original permissions and configurations.


Next Article
Article Tags :
Practice Tags :

Similar Reads