DML, DDL, DCL, and TCL commands in SQL Explained
Tags: sql, database, programming, tutorial
Published: 2022-11-28 10:02:35

## Introduction

SQL stands for "Structured Query Language." It is a standard computer language used to manipulate, store, and retrieve data from relational databases(RDBMS). 

Structured Query Language is the standard database language used by all Relational Database Management Systems (RDMS), including MySQL, MS Access, Oracle, Sybase, Informix, Postgres, and SQL Server. SQL performs the required tasks using commands such as Create, Drop, Insert, etc. 

These SQL commands are divided into four categories:

1. Data Definition Language (DDL)
2. Data Query Language (DQL)
3. Data Manipulation Language (DML)
4. Data Control Language (DCL)
5. Transaction Control Language (TCL)

Let's learn these commands one by one in brief.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbiig6z7stcdj4xumh0v.png)



> Note:- [RDBMS](https://www.oracle.com/in/database/what-is-a-relational-database/) stands for Relational DataBase Management Systems. It's a program that allows us to build, delete, and update relational databases. A relational database is a database system that retrieves and stores data in the form of rows and columns in a tabular format. It is a subset of the DBMS designed by E.F. Codd in the 1970s.

## Data Definition Language (DDL)
Data Definition Language or DDL commands in SQL are used to build and alter the database schema and its objects. In simple words, Data Definition Language(DDL) is a set of Structured Query Language commands used to create, update, and delete database structures but not data. 

The DDL commands deal with how data should be stored in the database. Generally, a general user shouldn't use these commands; instead, they should use an application to access the database. 

In SQL, there are five primarily used [DDL commands](https://www.scaler.com/topics/ddl-in-dbms/). These commands are listed below.

| Command  | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| CREATE   | It is a DDL command that is used to create databases, tables, triggers, and other database objects.<br /><br />**For Example**:- To create a database, we use the following command. <br /><br />CREATE DATABASE database_name; |                                                                                                                                                                                                                                                                     
| DROP     | It is a DDL command that is used to destroy or remove database objects from a SQL database. This DDL command lets us quickly delete the entire table, view, or index from the database.<br /><br />**For Example**:- To delete a table from the SQL database, we use the following command.<br /><br />DROP TABLE table_name;                                                                                                                                                                             |
| ALTER    | It is a DDL command this is used for Modifying and renaming aspects of an existing database table. This command can also be used to add and drop constraints from the table.<br /><br />**For Example**:- We use the following command to add a new field to the existing table.<br /><br />ALTER TABLE table_name ADD column_name column_definition;<br /><br />We use the following command to modify the column of the table:<br /><br />ALTER TABLE table_name MODIFY ( column_name column_datatype(size));                |
| TRUNCATE | TRUNCATE is another DDL command that deletes or eliminates all records from a table. This command also deletes the space set aside for storing table records. <br /><br />The TRUNCATE command does not have a WHERE clause, similar to the DROP command.<br /><br />TRUNCATE is faster than both DROP and DELETE. After using this command, we cannot roll back the data, just like with the DROP command.<br /><br />**For Example**:- We use the following command to delete the table.<br /><br />TRUNCATE TABLE table_name; |
| RENAME   | The DDL command RENAME is used to modify the name of a database table.<br /><br />**For Example**:- We use the following command to rename the table.<br /><br />RENAME TABLE old_table_name TO new_table_name;                                                                                                                                                                                                                                                                                              |

## Data Query Language (DQL)

Data Query Language or DQL command retrieves data from a database. The objective of the DQL Command is to obtain a schema relation based on the query given to it. It contains the [SELECT command](https://en.wikipedia.org/wiki/Select_(SQL)). 

With the help of this command, you can retrieve data from the database and manipulate it. When a SELECT command is executed on a table or tables, the result is compiled into a new temporary table, which is then displayed or received by the program, i.e., a front-end.

Data Query Language contains only one command.

| Command | Description                                                                                                                                                                                                                                                                                                                                  |
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| SELECT  | In SQL, the SELECT command or statement is used to retrieve data records from a database table and present them in the form of a result set. It is typically seen as a DQL command, although it can also be regarded as DML.<br /><br />The basic syntax of the SELECT command is:<br /><br />SELECT column_name1,…<br />FROM table_name<br />WHERE condition_ expression; |

## Data Manipulation Language (DML)

The Data Manipulation Language or DML commands in Structured Query Language modify the data in the SQL database. We can simply access, store, alter, update, and delete existing records in the database using DML commands. 

The three primarily used Data Manipulation Language (DML) commands in SQL are as follows.

| Command | Description                                                                                                                                                                                                                                                                                                                                                     |
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| INSERT  | It is an important data manipulation (DML) command in Structured Query Language that allows users to insert data into database tables.<br /><br />Syntax:<br /><br />INSERT INTO table_name ( column-1,column-2, …. column-N)<br />VALUES (value-1, value-2, value-3, .... value-N);                                                                                                   |
| DELETE  | SQL users can delete one or more existing records from the database tables using the DELETE DML command.<br /><br />This Data Manipulation Language command does not delete the stored data permanently from the database. We combine the DELETE command with the WHERE clause to choose particular rows from the table.<br /><br />Syntax:<br /><br />DELETE FROM table_Name WHERE condition; |
| UPDATE  | This DML command is used to change the value of a column in a Structured Query Language Database table.<br /><br />Syntax:<br /><br />UPDATE table_name SET<br />[column_name_1= value_1, column_name_2 = value_2 ...column_nameN = valueN] WHERE [CONDITION];                                                                                                                            |

## Data Control Language (DCL)
Data Control Language or DCL is associated with the commands used in  Structured Query Language that permit users to access, alter or work on the different privileges to control the database. 

This command also allows the database owner to give access, revoke access, and change the given permissions as and when required. DCL is basically used for enforcing [data security](https://www.ibm.com/in-en/topics/data-security).

There are two DCL commands present in SQL:

| Command | Description                                                                                                                                                                                                                                                                                                                                        |
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Grant   | GRANT is a DCL command that grants (give access to) security privileges to specific database users. It is mostly used to restrict user access to INSERT, DELETE, UPDATE, SELECT, EXECUTE, ALTER, or to provide user data privileges.<br /><br />The basic syntax of the GRANT command is:<br /><br />GRANT <privileges> ON <object name> TO <user/roles>               |
| Revoke  | REVOKE is a DCL command used to revoke given access granted via the GRANT command. It is mainly used to revert back to the time when no access was specified, i.e., withdrawing the permission that was authorized to carry out specific tasks.<br /><br />The basic syntax of the REVOKE command is:<br /><br />REVOKE <privileges> ON <object name> FROM <user/roles> |

## Transaction Control Language (TCL)
Transaction control language (TCL) commands are used to handle the transaction within the database. Transactions combine a collection of tasks into a single execution unit. 

Each transaction starts with some specific task and finishes when all of the tasks in the group are completed successfully. 

The transaction fails if any of the tasks fails. As a result, a transaction has only two outcomes, i.e., either success or failure. 

We use the following TCL commands to control the execution of a transaction:

| Command         | Description                                                                                                                                                                                                                                                                                                                                                       |
|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| COMMIT          | It is a TCL command that is used to save the data permanently. Any DML command, such as -INSERT, DELETE, or UPDATE, can be rolled back if the data is not permanently stored. To be on the safe side, we use the COMMIT command.<br /><br />Basic Syntax:<br /><br />commit;                                                                                                         |
| ROLLBACK        | The ROLLBACK command retrieves or restores data to the most recent savepoint or committed state. Suppose the data inserted, removed, or altered is incorrect for any reason.<br /><br />In that case, you can roll back the data to a specific savepoint, or if no savepoint is available, then the rollback is done from the last committed state.<br /><br />Basic Syntax:<br /><br />rollback |
| SAVEPOINT       | This command temporarily saves data at a specific point so it can be rolled back to that point if required.<br /><br />Basic Syntax:<br /><br />savepoint A;                                                                                                                                                                                                                        |
| SET TRANSACTION | This command specifies the transaction's characteristics.<br /><br />Basic Syntax:<br /><br />SET TRANSACTION [Read Write / Read Only];                                                                                                                                                                                                                                               |

## Conclusion
- SQL stands for "Structured Query Language." It is a standard computer language used to manipulate, store, and retrieve data from relational databases(RDBMS).
- The SQL commands are divided into four categories:

1. Data Definition Language (DDL)
2. Data Query Language (DQL)
3. Data Manipulation Language (DML)
4. Data Control Language (DCL)
5. Transaction Control Language (TCL)

- Data Definition Language(DDL) is a set of Structured Query Language commands used to create, update, and delete database structures but not data. Some of the common DDL commands are: CREATE, ALTER, DROP and TRUNCATE.
- Data Query Language or DQL command retrieves data from a database. It only includes the SELECT command.
- The Data Manipulation Language or DML commands in Structured Query Language modify the data in the SQL database. Some of the common DDL commands are: INSERT, UPDATE, and DELETE.
- Data Control Language or DCL is associated with the commands used in  Structured Query Language that permit users to access, alter or work on the different privileges to control the database. The two common DCL commands are GRANT and REVOKE.
- Transaction control language (TCL) commands are used to handle the transaction within the database. Some of the common DDL commands are COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION.