Home > Database > Mysql Tutorial > Check if user exists in MySQL and delete it?

Check if user exists in MySQL and delete it?

王林
Release: 2023-08-26 17:05:06
forward
885 people have browsed it

Check if user exists in MySQL and delete it?

To check how many users exist in MySQL, use the MySQL.user table. The syntax to check how many users are currently there is as follows.

mysql> SELECT User FROM mysql.user;
Copy after login
Copy after login

The following output shows the user -

+------------------+
| User             |
+------------------+
| Mac              |
| Manish           |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| Smith            |
| am               |
+------------------+
8 rows in set (0.00 sec)
Copy after login

Now you can check and delete the user if it exists.

The syntax to delete a user from MySQL is as follows -

DROP USER IF EXISTS yourUserName;
Copy after login

Now you can implement the above syntax to delete the user if it exists. I'm applying drop on user "Mac". The query is as follows.

mysql> DROP USER IF EXISTS Mac;
Query OK, 0 rows affected (0.11 sec)
Copy after login

Check if user "Mac" has been deleted. The query is as follows -

mysql> SELECT User FROM mysql.user;
Copy after login
Copy after login

The following is the output showing that the user "Mac" has been successfully deleted -

+------------------+
| User             |
+------------------+
| Manish           |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| Smith            |
| am               |
+------------------+
7 rows in set (0.00 sec)
Copy after login

The above is the detailed content of Check if user exists in MySQL and delete it?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template