mysql
  1. mysql-grant-privilege

Grant Privilege - (MySQL Privileges)

In MySQL, privileges are used to grant or revoke access to various database objects, such as tables and stored procedures. In this tutorial, we'll discuss how to grant privileges in MySQL using the GRANT statement.

Syntax

The syntax for the GRANT statement is as follows:

GRANT privilege [, privilege, ...]
    ON object
    TO user [, user, ...]
    [WITH GRANT OPTION]

Here's what each part of the statement means:

  • "privilege" is the name of the privilege you want to grant or revoke.
  • "object" is the name of the object to which you want to grant access.
  • "user" is the name of the user to whom you want to grant or revoke privileges.
  • "WITH GRANT OPTION" allows the user to grant the same privileges to other users.

Example

Let's say we want to grant a user named "john" the SELECT privilege on a table named "customers". Here's how we can do it:

GRANT SELECT ON customers
    TO john;

Now, the user "john" has the SELECT privilege on the "customers" table.

Output

When we run the example code above, there is no output generated by MySQL. However, we can verify that the privilege has been granted by running the SHOW GRANTS statement:

SHOW GRANTS FOR john;

This will show a list of all the privileges that have been granted to the user "john".

Explanation

In the example above, we used the GRANT statement to grant the SELECT privilege on the "customers" table to the user named "john". We can also see that the WITH GRANT OPTION was not specified, which means that "john" will not be able to grant this privilege to other users.

Use

The GRANT statement is used in MySQL to grant or revoke privileges to users on various database objects. This allows you to control access to your database, ensuring that users only have access to the objects they need.

Important Points

  • The GRANT statement allows you to grant or revoke privileges on database objects.
  • Privileges can be granted to specific users or to all users.
  • The WITH GRANT OPTION allows a user to grant the same privileges to other users.

Summary

In this tutorial, we discussed how to grant privileges in MySQL using the GRANT statement. We covered the syntax, example, output, explanation, use, and important points of the GRANT statement in MySQL. With this knowledge, you can now grant or revoke privileges to users on various database objects in MySQL.

Published on: