×

Grant Command in SQL

What is DCL (Data Control Language)?

Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of Structured Query Language (SQL). One of the logical groups of SQL Commands is Data Control Language.

The SQL commands known as Data Control Language (DCL) that allow a user to access, alter, or operate with various privileges in order to control the database. It enables the database owner to provide access, cancel access, and modify the permissions that have been granted as needed. The main purpose of DCL is to enforce data security.

What is the Grant command in SQL?

To define and access a database in SQL, various types of database languages are utilized. These languages enable us to update data and manage user access, among other things.

Data Control Language (DCL), one of these languages, is used to edit and retrieve data using particular SQL queries. It has two commands, revoke and grant, which are used to give and withdraw specific privileges to users in a multi-user database.

In layman's terms, we can say that we can manage a user's permissions and rights with the aid of these commands.

Grant Command

When a user needs to alter or retrieve a database object like a table, view, sequence, synonym, or index, they can use the SQL command to grant them the necessary permissions or privileges.

Another benefit of using this command is that a third user will also receive the same permissions.

Grant Command in SQL

Different types of privileges given by grant command

Syntax of Grant command in SQL

The syntax of the Grant command is as follows:

GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];

There are some parameters of the Grant command:

  • privilege_name: It displays the authorization that must be given.
  • object_name: The name of the database object, such as a view, table, index, etc., is represented by this.
  • user name: It stands for the user who has to be granted permission.
  • PUBLIC: This signifies that all database users have access.
  • role_name: It indicates that users who fit a certain role have access to these resources.

Examples of Grant Command in SQL

Let's say we have a database related to the university where we can establish a table with the following information about the student and their registered course are as follows:

CREATE TABLE Student(stud_name varchar(30), roll_no varchar(10), course_enrolled varchar(30));

A table with the three columns student name (stud_name), roll number (roll_no), and course enrollment (course_enrolled) will be generated as a result of this command.

Now let's insert the values in the table:

INSERT INTO Student
VALUES("Rohit", "SG19333", "Bachelors’s of Computer Application"),
VALUES("Akash","SG19444", "Computer Science & Engineering"),
VALUES("Nikhar", "SG19999", "Information Technology"),
VALUES("Yashraj","SG15555", "Software Engineering");

Using the SELECT command, the following table can be seen:

SELECT * from Student

Output:

stud_nameroll_nocourse_enrolled
RohitSG19333Bachelors’s of Computer Application
AkashSG19444Computer Science & Engineering
NikharSG19999Information Technology
YashrajSG15555Software Engineering

To grant permission to a single user: The following command is used to grant the college's director the SELECT permission.

GRANT SELECT 
ON Student
TO Director

To grant permission to the public: To give all database users the SELECT access, we use the following command.

GRANT SELECT
ON Student
TO PUBLIC

WITH GRANT OPTION Clause

With the use of the WITH GRANT OPTION clause, a user who already has access to a certain table may grant that access to additional users as well.

For instance, in the university database situation mentioned above, if we use this clause to grant the Director the SELECT permission, then the Director can grant those permissions to any user of the database.

GRANT SELECT
ON STUDENT
TO Director
WITH GRANT OPTION

To grant all permissions to a particular user

Either we can mention each keyword one by one following the grant keyword or we can use the ALL keyword to allow the specified user to do all operations in order to grant them all permissions.

GRANT SELECT, INSERT, UPDATE, DELETE
ON Student
TO Director
Or we can also use this command
GRANT ALL
ON Student
TO Director

Related Topics

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.