SQL / SQL Security and Access Control

Using GRANT and REVOKE Effectively

This tutorial will focus on using the GRANT and REVOKE commands effectively for managing database access.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explores securing SQL databases and managing user access.

Using GRANT and REVOKE Effectively

1. Introduction

In this tutorial, our goal is to understand how to effectively use the GRANT and REVOKE commands in SQL, which are crucial for managing database access.

By the end of this tutorial, you will be able to:
- Understand the purpose of the GRANT and REVOKE commands.
- Use these commands to control permissions on a database.

The only prerequisite for this tutorial is basic knowledge of SQL.

2. Step-by-Step Guide

The GRANT command is used to allow users to perform specific tasks on a database, like SELECT, INSERT, UPDATE, DELETE, and others. Conversely, the REVOKE command is used to take back the permissions granted to users.

GRANT Command

The basic syntax of the GRANT command is:

GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
  • privilege_name is the access right or privilege granted to the user.
  • object_name is the name of the database object like TABLE, VIEW, etc.
  • WITH GRANT OPTION allows a user to grant or revoke privileges from other users too.

REVOKE Command

The basic syntax of the REVOKE command is:

REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
  • privilege_name is the access right or privilege to be taken back.
  • object_name is the name of the database object like TABLE, VIEW, etc.

3. Code Examples

Example 1: Granting privileges

GRANT SELECT, INSERT, DELETE ON my_table TO user1;

This command grants user1 SELECT, INSERT, and DELETE rights on my_table.

Example 2: Revoking privileges

REVOKE INSERT ON my_table FROM user1;

This command revokes user1’s INSERT rights on my_table.

4. Summary

In this tutorial, you have learned how to use the GRANT and REVOKE commands to manage database access. These commands are critical for maintaining database security by controlling the actions that users can perform.

For further learning, you should look into the different types of privileges that can be granted or revoked and how to manage groups of users using roles.

5. Practice Exercises

  1. Create a database and a table within it. Grant SELECT, INSERT, and UPDATE privileges to a new user on this table.

  2. After completing the first exercise, revoke the UPDATE privilege from this new user.

  3. Grant all privileges to a new user on a new table. After that, revoke all privileges from this user.

Solutions:

CREATE DATABASE my_database;
USE my_database;
CREATE TABLE my_table (column1 INT, column2 VARCHAR(20));
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE ON my_table TO 'new_user'@'localhost';
REVOKE UPDATE ON my_table FROM 'new_user'@'localhost';
CREATE TABLE another_table (column1 INT, column2 VARCHAR(20));
CREATE USER 'another_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON another_table TO 'another_user'@'localhost';
REVOKE ALL ON another_table FROM 'another_user'@'localhost';

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help