100% found this document useful (1 vote)
76 views10 pages

Understanding SQL: DDL, DML, DCL, TCL

The document defines and provides examples of the main SQL sublanguages: DDL (Data Definition Language) for defining database schemas, DML (Data Manipulation Language) for manipulating data, DCL (Data Control Language) for controlling access privileges, and TCL (Transaction Control Language) for managing transactions. DDL commands include CREATE, DROP, ALTER, and RENAME. DML commands are SELECT, INSERT, UPDATE, and DELETE. DCL commands are GRANT and REVOKE. TCL commands are START TRANSACTION, COMMIT, and ROLLBACK.

Uploaded by

Abdulazeez Rabo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
76 views10 pages

Understanding SQL: DDL, DML, DCL, TCL

The document defines and provides examples of the main SQL sublanguages: DDL (Data Definition Language) for defining database schemas, DML (Data Manipulation Language) for manipulating data, DCL (Data Control Language) for controlling access privileges, and TCL (Transaction Control Language) for managing transactions. DDL commands include CREATE, DROP, ALTER, and RENAME. DML commands are SELECT, INSERT, UPDATE, and DELETE. DCL commands are GRANT and REVOKE. TCL commands are START TRANSACTION, COMMIT, and ROLLBACK.

Uploaded by

Abdulazeez Rabo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Table of Contents

What is DDL in SQL?

CREATE

DROP

ALTER

RENAME

TRUNCATE

What is DML in SQL?

SELECT

INSERT

UPDATE

DELETE

What is DCL in SQL?

GRANT

REVOKE

What is TCL in SQL?

START TRANSACTION (BEGIN, BEGIN WORK)

COMMIT

ROLLBACK

First of all, let’s define what is DDL, DML, DCL, and TCL in DBMS.

DDL is Data Definition Language

DML is Data Manipulation Language

DCL is Data Control Language


TCL is Transaction Control Language

As you see from its name it allows to define, manipulate and control data and transactions in SQL
language.

It’s four types of SQL sub-languages, that’s why it’s no sense to search for a difference between DDL vs
DML or DCL vs TCL.

SQL commands list:

Language Command List

DDL CREATE

DROP

ALTER

RENAME

TRUNCATE

DML SELECT

INSERT

UPDATE

DELETE

DCL GRANT

REVOKE

TCL START TRANSACTION

COMMIT

ROLLBACK

Keep reading and I’ll explain in details what are DDL, DML, DCL, and TCL with examples.
What is DDL in SQL?

DDL allows you to create SQL statements to make operations with database data structures (schemas,
tables etc.).

These are SQL DDL commands list and examples:

CREATE

CREATE statement is used to create a new database, table, index or stored procedure.

Create database example:

CREATE DATABASE explainjava;

Create table example:

CREATE TABLE user (

id INT(16) PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(255) NOT NULL

);

DROP

DROP statement allows you to remove database, table, index or stored procedure.

Drop database example:

DROP DATABASE explainjava;

Drop table example:


DROP TABLE user;

ALTER

ALTER is used to modify existing database data structures (database, table).

Alter table example:

ALTER TABLE user ADD COLUMN lastname VARCHAR(255) NOT NULL;

RENAME

RENAME command is used to rename SQL table.

Rename table example:

RENAME TABLE user TO student;

TRUNCATE

TRUNCATE operation is used to delete all table records.

Logically it’s the same as DELETE command.

Differences between DELETE and TRUNCATE commands are:

TRUNCATE is really faster

TRUNCATE cannot be rolled back

TRUNCATE command does not invoke ON DELETE triggers

Example:
TRUNCATE student;

What is DML in SQL?

DML is a Data Manipulation Language, it’s used to build SQL queries to manipulate (select, insert,
update, delete etc.) data in the database.

This is DML commands list with examples:

SELECT

SELECT query is used to retrieve a data from SQL tables.

Example:

SELECT * FROM student;

INSERT

INSERT command is used to add new rows into the database table.

Example:

INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');

UPDATE

UPDATE statement modifies records into the table.

Example:

UPDATE student SET name = 'Dima' WHERE lastname = 'Shvechikov';

DELETE
DELETE query removes entries from the table.

Example:

DELETE FROM student WHERE name = 'Dima';

What is DCL in SQL?

DCL a Data Control Language.

Its commands are responsible for access restrictions inside of the database.

Let’s take a look at DCL statements definitions.

GRANT

GRANT command gives permissions to SQL user account.

For example, I want to grant all privileges to ‘explainjava’ database for user ‘dmytro@localhost’.

Let’s create a user first:

CREATE USER 'dmytro'@'localhost' IDENTIFIED BY '123';

Then I can grant all privileges using GRANT statement:

GRANT ALL PRIVILEGES ON explainjava.* TO 'dmytro'@'localhost';

and we have to save changes using FLUSH command:


FLUSH PRIVILEGES;

REVOKE

REVOKE statement is used to remove privileges from user accounts.

Example:

REVOKE ALL PRIVILEGES ON explainjava.* FROM 'dmytro'@'localhost';

and save changes:

FLUSH PRIVILEGES;

What is TCL in SQL?

TCL is a Transaction Control Language.

Its commands are used to manage transactions in SQL databases.

This is TCL commands list:

START TRANSACTION (BEGIN, BEGIN WORK)

START TRANSACTION is used to start a new SQL transaction.

BEGIN and BEGIN WORK are aliases for START TRANSACTION.

Example:

START TRANSACTION;
after that, you’re doing manipulations with a data (insert, update, delete) and at the end, you need to
commit a transaction.

COMMIT

As a mentioned above COMMIT command finishes transaction and stores all changes made inside of a
transaction.

Example:

START TRANSACTION;

INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');

COMMIT;

ROLLBACK

ROLLBACK statement reverts all changes made in the scope of transaction.

Example:

START TRANSACTION;

INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');

ROLLBACK;

DDL(Data Definition Language) : DDL or Data Definition Language actually consists of the SQL commands
that can be used to define the database schema. It simply deals with descriptions of the database
schema and is used to create and modify the structure of database objects in the database.

Examples of DDL commands:

CREATE – is used to create the database or its objects (like table, index, function, views, store procedure
and triggers).
DROP – is used to delete objects from the database.

ALTER-is used to alter the structure of the database.

TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are
removed.

COMMENT –is used to add comments to the data dictionary.

RENAME –is used to rename an object existing in the database.

DQL (Data Query Language) :

DQL statements are used for performing queries on the data within schema objects. The purpose of the
DQL Command is to get some schema relation based on the query passed to it.

Example of DQL:

SELECT – is used to retrieve data from the database.

DML(Data Manipulation Language): The SQL commands that deals with the manipulation of data
present in the database belong to DML or Data Manipulation Language and this includes most of the
SQL statements.

Examples of DML:

INSERT – is used to insert data into a table.

UPDATE – is used to update existing data within a table.

DELETE – is used to delete records from a database table.

DCL(Data Control Language): DCL includes commands such as GRANT and REVOKE which mainly deal
with the rights, permissions and other controls of the database system.

Examples of DCL commands:

GRANT-gives user’s access privileges to the database.

REVOKE-withdraw user’s access privileges given by using the GRANT command.


TCL(transaction Control Language): TCL commands deal with the transaction within the database.

Examples of TCL commands:

COMMIT– commits a Transaction.

ROLLBACK– rollbacks a transaction in case of any error occurs.

SAVEPOINT–sets a savepoint within a transaction.

SET TRANSACTION–specify characteristics for the transaction.

Common questions

Powered by AI

Transaction Control Language (TCL) plays a crucial role in managing database transactions, ensuring that operations can be completed successfully or reverted if necessary. COMMIT saves all changes made in a transaction permanently, confirming that all operations within that transaction are successfully applied to the database. This prevents any data loss or inconsistency issues after operations are executed. In contrast, ROLLBACK undoes all changes made in the current transaction, reverting the database to its previous state before the transaction began if errors occur or a manual rollback is required. TCL thus enables robust transaction management through precise control over database changes .

GRANT and REVOKE commands in Data Control Language (DCL) manage user permissions for accessing database resources. GRANT assigns specific privileges to users, such as granting access to a particular database or specifying operations a user can perform. For example, GRANT ALL PRIVILEGES ON explainjava.* TO 'dmytro'@'localhost'; assigns all permissions on the explainjava database to the specified user. Conversely, REVOKE withdraws previously granted permissions, such as REVOKE ALL PRIVILEGES ON explainjava.* FROM 'dmytro'@'localhost';, which removes all existing permissions from the user .

Within Data Query Language (DQL), the SELECT command is pivotal for retrieving data from database tables. It allows users to specify particular columns or entire tables from which to gather information, thus facilitating data analysis and reporting. For example, SELECT * FROM student; retrieves all columns for each record in the 'student' table, while SELECT name FROM student; fetches only the 'name' column. Select queries can be expanded with conditions or joins to fine-tune data extraction, highlighting the command's critical role in accessing and viewing data in databases .

The DROP command in SQL completely removes database objects such as tables or indexes, leading to a permanent loss of both structure and data, which cannot be undone. For example, DROP TABLE student; deletes the 'student' table and all its data entirely. Conversely, the ALTER command modifies existing database structures but retains the data within them. It allows adjustments such as adding or removing columns, e.g., ALTER TABLE student ADD COLUMN grade INT;, without losing existing records. Thus, while DROP significantly alters the database by removing elements, ALTER enables structural changes without affecting stored data, providing flexible reconfiguration while maintaining data integrity .

The TRUNCATE command is practical for removing all records from a table rapidly without logging individual deletion operations, offering enhanced performance over the DELETE command for bulk deletions. However, its use also has limitations: TRUNCATE cannot be rolled back once executed, unlike DELETE within a transaction, leading to an inability to recover deleted data if necessary. It also bypasses triggers and does not maintain the same transaction logging as DELETE, which may be critical in applications tracking changes for auditing purposes. Therefore, while TRUNCATE is efficient for clearing tables quickly, it sacrifices control, auditing, and rollback capabilities, which may limit its applicability in scenarios where these features are essential .

START TRANSACTION is fundamental to maintaining data integrity by initializing a new transaction, thereby creating a controlled environment for a series of SQL operations, and ensuring that either all operations succeed or none are applied. It isolates transactional changes until a COMMIT is executed to save them permanently, guaranteeing atomicity. If errors occur or integrity is compromised, ROLLBACK can undo all operations, reverting changes before the START TRANSACTION command. This ensures consistency, reinforcing database reliability by preventing partial application of conflicting changes, and is pivotal for transaction management processes focusing on accurate and complete database state transitions .

The COMMIT command is crucial after executing a GRANT command because it finalizes permission changes in the database, ensuring that any modifications to user access privileges are saved permanently. Without committing, changes made by a GRANT might not persist beyond the current transaction session, risking inconsistent or incomplete permission allocation. Committing these alterations guarantees that the intended access controls are applied, maintaining database security and user operation integrity. Therefore, performing a COMMIT ensures that critical DCL modifications, like GRANT, are retained and active across database interactions .

DDL (Data Definition Language) commands, such as CREATE, DROP, ALTER, and TRUNCATE, are used to define and manipulate the structure of database objects, such as tables and schemas. This makes DDL operations focus on the database schema itself. On the other hand, DML (Data Manipulation Language) commands, including SELECT, INSERT, UPDATE, and DELETE, are utilized to manipulate data within the existing database structures. Essentially, while DDL focuses on the structure, DML deals with the alteration and retrieval of the actual data stored in these structures .

Although TRUNCATE and DELETE both remove records from a table, they operate differently. TRUNCATE is a DDL command that quickly removes all rows in a table without logging individual row deletions, making it faster than DELETE. It cannot be rolled back and does not activate triggers linked to DELETE operations. In contrast, DELETE is a DML command that removes rows one at a time, allowing specific deletions and rollback in transactions, retaining granular control and activating triggers. Consequently, TRUNCATE is preferable for bulk deletions when rollback is unnecessary, whereas DELETE is better for specific or conditional deletions that may need to be rolled back .

To rename an existing table in SQL, the RENAME command is used. It changes the name of a table without altering its structure. For example, RENAME TABLE user TO student; changes the table name from 'user' to 'student.' In contrast, the ALTER command modifies the structure of a table, such as adding or removing columns, rather than changing the table's name. For instance, ALTER TABLE student ADD COLUMN lastname VARCHAR(255) NOT NULL; adds a new column to the table. Therefore, RENAME focuses specifically on changing table names, whereas ALTER deals with modifying table attributes or structure .

You might also like