DEV Community

Cover image for Mastering DCL: The Ultimate Guide to GRANT and REVOKE in SQL
Felipe Cezar
Felipe Cezar

Posted on

Mastering DCL: The Ultimate Guide to GRANT and REVOKE in SQL

Introduction

If you work with databases—whether you are focused on Software Engineering, Data Science, or managing mission-critical DBs in the financial sector—you know that security is not optional. You can't just let any user drop a table in your production environment, right?

This is where DCL (Data Control Language) steps in. While DML manipulates data and DDL defines structures, DCL acts as the bouncer of your database. It decides who gets in, where they can go, and what they are allowed to do.

Today, we are going to break down the two absolute powerhouse commands in this category: GRANT and REVOKE.


The Golden Rule: The Basic Structure

The beauty of DCL is that its syntax is highly logical. Think of it as a straightforward English sentence:
Action + What is allowed/forbidden + Where + To whom/From whom.

Both GRANT and REVOKE follow this exact same backbone.


1. GRANT: Handing Out the Access Badge

The GRANT command is used to give privileges to a user or a role (a group of users).

Basic Syntax:

GRANT [privileges] ON [table/view] TO [user];
Enter fullscreen mode Exit fullscreen mode

Practical Example 1: Read and Write Access

Imagine a new junior HR analyst just joined the team. They need to read data and insert new employees, but we don't want them deleting anything.

GRANT SELECT, INSERT ON HR05_EMPLOYEE TO junior_analyst;
Enter fullscreen mode Exit fullscreen mode

Practical Example 2: Full Access (God Mode)

If you need to give complete control over a specific table to a senior developer:

GRANT ALL PRIVILEGES ON HR05_EMPLOYEE TO senior_dev;
Enter fullscreen mode Exit fullscreen mode

The Pro Tip: WITH GRANT OPTION

What if you want the user to receive the permission and also be able to pass it on to others? We use the WITH GRANT OPTION. This is very common when delegating schema administration to a tech lead.

GRANT SELECT ON SALES_TABLE TO tech_lead WITH GRANT OPTION;
Enter fullscreen mode Exit fullscreen mode

2. REVOKE: Taking the Badge Back

REVOKE does the exact opposite. It's the security command used to remove specific permissions without needing to delete the user from the system entirely.

Basic Syntax:

REVOKE [privileges] ON [table/view] FROM [user];
Enter fullscreen mode Exit fullscreen mode

(Notice that we switch from TO to FROM. You grant TO someone, and you revoke FROM someone).

Practical Example 1: Surgical Privilege Removal

Imagine the user HR5678 made a mistake and can no longer have the permission to delete rows from the employee table, but they must still be able to query and insert data. We strip away only the DELETE privilege:

REVOKE DELETE ON HR05_EMPLOYEE FROM HR5678;
Enter fullscreen mode Exit fullscreen mode

Practical Example 2: Cutting Everything Off

If an employee changed departments or a system was compromised, we cut everything at once:

REVOKE ALL PRIVILEGES ON HR05_EMPLOYEE FROM HR5678;
Enter fullscreen mode Exit fullscreen mode

The Ripple Effect (CASCADE vs RESTRICT)

Remember the WITH GRANT OPTION? If the tech lead granted permissions to 5 interns, and you revoke the tech lead's access, what happens to the interns?
In many DBMSs (like Oracle), revoking defaults to a cascading effect. If the "root" loses the permission, all the branches that received the permission through it lose it as well.


Best Practices: The Principle of Least Privilege (PoLP)

In the real world (and in good architectures), we never give out permissions "just in case". The golden rule is the Principle of Least Privilege: the user should have only the strictly necessary permissions to perform their job, and nothing more.

  • Plugging a PowerBI dashboard or extracting data for Data Science? The DB connection user only needs GRANT SELECT.
  • A web app needs to update customer registration? GRANT UPDATE.

This way, if a password leaks or poorly written code is deployed, the blast radius is contained within the DCL "fences".


Did you enjoy this overview? What other database commands give you a headache when setting up access control? Let me know in the comments!

Top comments (0)