SQL (Structured Query Language) includes several data control language (DCL) statements that are used to control access to data in a relational database management system (RDBMS). The two primary DCL statements in SQL are:
GRANT: The GRANT statement is used to give specific privileges or permissions to database users or roles. These privileges include the ability to SELECT, INSERT, UPDATE, DELETE, or execute other SQL statements on specific database objects (e.g., tables, views, procedures). The syntax for GRANT typically looks like this:
Table of Contents
ToggleGRANT SELECT, INSERT ON employees TO user1;
REVOKE SELECT ON employees FROM user1;
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';After creating the user, you may also need to grant specific privileges to that user, such as granting access to a particular database or specific permissions on database objects. You can use the GRANT statement for this purpose. Here’s a basic example of granting all privileges on a specific database to the ‘newuser’:
GRANT ALL PRIVILEGES ON dbname.* TO 'newuser'@'%';SELECT: Allows users to retrieve data from one or more tables in a database.
GRANT SELECT ON database_name.table_name TO 'username'@'host';UPDATE: Allows users to modify existing data in a table.
GRANT UPDATE ON database_name.table_name TO 'username'@'host';DELETE: Allows users to remove rows from a table.
GRANT DELETE ON database_name.table_name TO 'username'@'host';CREATE: Allows users to create new databases or tables.
GRANT CREATE ON database_name.* TO 'username'@'host';DROP: Allows users to delete databases or tables.
GRANT DROP ON database_name.* TO 'username'@'host';ALTER: Allows users to modify the structure of existing tables.
GRANT ALTER ON database_name.table_name TO 'username'@'host';ALL PRIVILEGES: Grants all available privileges on a specific database or table.
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
