SQL Commands

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

SQL Commands

DDL, DQL, DML, DCL and TCL


Commands

Ashish Zope
Data Engineer at Bajaj Finserv
SQL Commands (DDL, DQL, DML, DCL and TCL)
➢ SQL commands are extensively used to interact with databases,
enabling users to perform a wide range of actions on database
systems.

➢ Understanding these commands is crucial for effectively managing


and manipulating data.

➢ SQL Commands are mainly categorized into five categories:

1. DDL – Data Definition Language


2. DQL – Data Query Language
3. DML – Data Manipulation Language
4. DCL – Data Control Language
5. TCL – Transaction Control Language
Types of SQL commands
SQL Commands

DDL DML DQL DCL TCL

CREATE INSERT SELECT GRANT COMMIT

ALTER UPDATE REVOKE SAVEPOINT

DROP DELETE ROLL BACK

SET
TRUNCATE
TRANSACTION
Data Definition Language (DDL)

➢ Definition:
DDL, or Data Definition Language, consists of SQL commands
used to define the database schema. It deals with
descriptions of the database schema and is used to create
and modify the structure of database objects in the database.
➢ Purpose:
DDL is a set of SQL commands used to create, modify, and
delete database structures, but not the data within them.
These commands are typically used by database
administrators and developers rather than general users, who
usually access the database through an application.
➢ Key DDL Commands:
CREATE, DROP, TRUNCATE and ALTER
Data Definition Language (DDL)
1. CREATE
Purpose: Used to create databases, tables, indexes, and views.
Syntax:

-- CREATE DATABASE
CREATE DATABASE database_name;

-- CREATE TABLE
CREATE TABLE table_name (
column1 datatype,
);

-- CREATE INDEXES
CREATE INDEX index_name ON table_name (column1, column2,
...);

-- CREATE VIEW
CREATE VIEW view_name AS
SELECT column1, column2, ... FROM table_name WHERE condition;
Data Definition Language (DDL)
2. ALTER
Purpose: Used to modify existing database objects like tables.
Syntax:

-- TO ADD NEW COLUMN IN TABLE


ALTER TABLE table_name
ADD column_name datatype;

-- TO MODIFY THE COLUMN OF TABLE


ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

-- TO DROP COLUMN FROM TABLE


ALTER TABLE table_name
DROP COLUMN column_name;
Data Definition Language (DDL)
3. DROP
Purpose: Used to delete databases, tables, indexes, or views.
Syntax:

-- DROP DATABASE
DROP DATABASE database_name;

-- DROP TABLE
DROP TABLE table_name;

-- DROP VIEW
DROP VIEW view_name;

-- TO DROP INDEX
DROP INDEX index_name;
Data Definition Language (DDL)
4. TRUNCATE
Purpose: Used to delete all records from a table, but not the table itself.
Syntax:

-- DROP TRUNCATE
TRUNCATE TABLE table_name;
Data Manipulation Language (DML)

➢ Definition:
DML, or Data Manipulation Language, consists of SQL
commands used to manipulate data stored in a database. It
includes commands for inserting, updating, deleting, and
retrieving data.

➢ Purpose:
DML is used to manage data within database objects. These
commands allow users to perform operations such as adding
new records, modifying existing records, and removing
records.

➢ Key DDL Commands:


INSERT, UPDATE, DELETE
Data Manipulation Language (DML)
1. INSERT
Purpose: Used to add new records to a table.
Syntax:

-- TO INSERT THE DATA/RECORD INTO TABLE


INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Data Manipulation Language (DML)
2. DELETE
Purpose: Used to remove records from a table.
Syntax:

-- TO REMOVE THE DATA/RECORD FROM TABLE


DELETE FROM table_name
WHERE condition;
Data Manipulation Language (DML)
3. UPDATE
Purpose: Used to modify existing records in a table.
Syntax:

-- TO UPDATE THE RECORD IN TABLE


UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Data Query Language (DQL)

➢ Definition:
DQL, or Data Query Language, consists of SQL commands
used to query and retrieve data from a database. It focuses on
the retrieval of data based on specific criteria.

➢ Purpose:
DQL is used to fetch data from the database. The primary
command in DQL is `SELECT`, which allows users to specify
the columns and conditions for data retrieval.

➢ Key DDL Commands:


SELECT
Data Query Language (DQL)
SELECT
Purpose: Used to query and retrieve data from a database.
Syntax:

-- TO RETRIVE DATA FROM SQL TABLE


SELECT column1, column2, ...
FROM table_name
WHERE condition;
Data Control Language (DCL)

➢ Definition:
DCL, or Data Control Language, consists of SQL commands used
to control access to data in a database. It includes commands for
granting and revoking permissions to users and roles.

➢ Purpose:
DCL is used to manage access rights and permissions for database
users. These commands ensure that only authorized users can
perform certain actions on the database objects.

➢ Key DDL Commands:


GRANT, ROVOKE
Data Control Language (DCL)
1. GRANT
Purpose: Used to give users access privileges to the database.
Syntax:

-- TO GIVE USERS ACCESS PRIVILEGES


GRANT privilege_type
ON object_name
TO user_name;
Data Control Language (DCL)
2. REVOKE
Purpose: Used to remove access privileges from users.
Syntax:

-- TO REMOVE ACCESS PRIVILEGES FROM USERS.


REVOKE privilege_type
ON object_name
FROM user_name;
Transaction Control Language (TCL)

➢ Definition:
TCL, or Transaction Control Language, consists of SQL commands
used to manage transactions in a database. Transactions are
sequences of operations performed as a single logical unit of
work.

➢ Purpose:
TCL is used to ensure the integrity of data by managing the
changes made by DML statements. These commands help in
maintaining consistency and handling errors during data
manipulation.

➢ Key DDL Commands:


COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION
Transaction Control Language (TCL)
1. COMMIT
Purpose: Used to save all changes made during the current transaction.
Syntax:

-- TO OMPLETE THE TRANSACTION/SAVE THE DETAILS


COMMIT;
Transaction Control Language (TCL)
2. SAVEPOINT
Purpose: Used to set a savepoint within a transaction, allowing partial
rollback.
Syntax:

-- TO SET A SAVEPOINT WITHIN A TRANSACTION


SAVEPOINT savepoint_name;
Transaction Control Language (TCL)
3. ROLLBACK
Purpose: Used to undo changes made during the current transaction.
Syntax:

-- TO UNDO CHANGES MADE DURING THE CURRENT TRANSACTION.


ROLLBACK;
Transaction Control Language (TCL)
4. SET TRANSACTION
Purpose: Used to specify characteristics for the transaction, such as
isolation level.
Syntax:

-- TO SPECIFY CHARACTERISTICS FOR THE TRANSACTION


SET TRANSACTION READ ONLY;
Ashish Zope
THANK YOU Data Engineer at Bajaj Finserv

If you find this helpful, Repost and follow for more content.
Submit corrections in comments, if any.

You might also like