What are SQL DCL(Data Control Language) Commands?

Mounish V Last Updated : 24 Jun, 2024
3 min read

Introduction

Data Control Language (DCL) commands are fundamental in SQL for managing access and permissions within the database. These commands allow database administrators to regulate data access for various users, ensuring security and effective data management. This article discusses the primary DCL commands, their purposes, and practical applications.

SQL Command

Overview

  • Understand the role and importance of SQL DCL commands.
  • Explore the common DCL commands: GRANT and REVOKE.
  • Learn how these commands help in managing database security and user permissions.
  • Examine practical examples to understand their applications better.

Applications of DCL Commands

Let’s start with creating a database, a table, and a user:

CREATE DATABASE company_db;

USE company_db;

CREATE TABLE employees (

    employee_id INT PRIMARY KEY,

    first_name VARCHAR(50),

    last_name VARCHAR(50),

    email VARCHAR(100),

    hire_date DATE

);

CREATE USER 'user1'@'localhost' IDENTIFIED BY 'password';

This query created a database company_db, an empty table of employees, and a user named user1

GRANT Command

The GRANT command provides specific privileges to users or roles. These privileges can include selecting, inserting, updating, or deleting data and administrative permissions like creating tables or databases.

Granting Select Privilege

To grant a user the ability to read data from a table, use the following command:

GRANT SELECT ON employees TO 'user1'@'localhost';

This command gives `user1` the permission to perform SELECT queries on the `employees` table.

Granting Multiple Privileges

You can also grant multiple privileges in a single command:

GRANT SELECT, INSERT, UPDATE ON employees TO 'user1'@'localhost';

This command grants `user1` permission to select, insert, and update data in the `employees` table.

Granting Administrative Privileges

To allow a user to create new tables in a database, use:

GRANT CREATE ON company_db TO 'user1'@'localhost';

This command provides `user1` the ability to create new tables within the `company_db` database.

REVOKE Command

The REVOKE command removes previously granted privileges from users or roles. This is crucial for maintaining security and ensuring that users only have the necessary access.

Revoking a Specific Privilege

To remove a specific privilege, use the following command:

REVOKE SELECT ON employees FROM 'user1'@'localhost';
SQL DCL Commands Data Control Language

This command removes the SELECT permission from `user1` on the `employees` table.

Revoking All Privileges

To revoke all privileges previously granted to a user, use:

REVOKE ALL PRIVILEGES ON employees FROM 'user1'@'localhost';

This command revokes all permissions that `user1` had on the `employees` table.

Conclusion

SQL DCL commands play a critical role in database security and management. Through GRANT and REVOKE, database administrators can accurately manage user access, protecting sensitive data and providing users with necessary permissions. These DCL commands are vital for a secure and well-administered database.

Also Read: SQL: A Full Fledged Guide from Basics to Advance Level

Frequently Asked Questions

Q1. How to revoke a user’s permission to insert data into a table?

A. REVOKE command can be used to remove the INSERT privilege from a user:    
REVOKE INSERT ON employees FROM user1;

Q2. Can I grant multiple privileges at once?

A. Yes, you can grant multiple privileges in a single command. For example:
    GRANT SELECT, INSERT, UPDATE ON employees TO user1;

Q3. How do I grant administrative privileges to a user?

A. To grant administrative privileges, such as creating tables, you can use the GRANT command with the appropriate privilege:
GRANT CREATE ON DATABASE company_db TO user1;

I'm a tech enthusiast, graduated from Vellore Institute of Technology. I'm working as a Data Science Trainee right now. I am very much interested in Deep Learning and Generative AI.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details