×

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 SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 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.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 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 INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.