Selected topic

GRANT

Data Control Language Dcl

Prefer practical output? Use related tools below while reading.

### GRANT

Purpose: Grant privileges to a user or role.

Syntax:


sql
GRANT privilege ON object_name TO {user|role};

Example:
sql
-- Grant SELECT privilege on table "employees" to user "john"
GRANT SELECT ON employees TO john;

In this example, the GRANT command is used to grant the SELECT privilege on the employees table to the user named john.

Privileges:


  • SELECT: allows a user to read data from a table.
  • INSERT: allows a user to insert new records into a table.
  • UPDATE: allows a user to modify existing records in a table.
  • DELETE: allows a user to delete records from a table.
  • ALL PRIVILEGES: grants all privileges on an object.

Roles:


You can also grant privileges to roles, which are groups of users that have similar access requirements. Roles make it easier to manage permissions by allowing you to assign privileges to a group rather than individual users.

sql
-- Grant SELECT privilege on table "employees" to role "hr_staff"
GRANT SELECT ON employees TO hr_staff;
Note that roles can be granted only in the context of an object, not globally.