
SQL Command Types Explained: DDL, DML, DCL, and TCL
The overview of the four main categories of SQL commands: Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). Each category serves a distinct purpose in database management, from defining the database structure to controlling access and managing transactions. Understanding these command types is crucial for effective database administration and development
1. DDL – Data Definition Language
Purpose:
DDL commands are used to define and modify the structure of database objects. These
objects include tables, schemas, views, indexes, and other database elements. DDL
commands are responsible for creating, altering, and deleting these structures.
Common Commands:
CREATE
– Creates a new database object, such as a table, view, index, or schema.ALTER
– Modifies an existing database object, allowing changes to its structure or properties.DROP
– Deletes a database object, removing it from the database.
TRUNCATE
– Removes all records from a table quickly. This operation cannot be rolled back
and is faster than using a DELETE statement without a WHERE clause.
2. DML – Data Manipulation Language
Purpose:
DML commands are used to manage the data within database tables. These commands allow you to insert, update, and delete data, as well as retrieve data for analysis and reporting.Common Commands:
INSERT
– Adds new rows of data into a table.UPDATE
– Modifies existing data within a table.DELETE
– Removes rows of data from a table.SELECT
– Retrieves data from one or more tables based on specified criteria. (Although SELECT is technically a DQL- Data Query Language- command, it is often grouped with DML due to its data manipulation aspect.)
3. DCL – Data Control Language
DCL commands are used to control access to data within the database. These commands manage user permissions and security, ensuring that only authorized users can access and modify specific data.
Common Commands:
GRANT
– Gives specific permissions to users or roles, allowing them to perform certain actions on database objects.REVOKE
– Removes previously granted permissions from users or roles, restricting their access to database objects.
4. TCL – Transaction Control Language
Purpose:
TCL commands are used to manage transactions within the database. Transactions are
sequences of operations that are treated as a single unit of work. TCL commands allow you
to control the execution and outcome of these transactions, ensuring data consistency and
integrity.
Common Commands:
COMMIT
– Saves all changes made during the current transaction permanently to the database.ROLLBACK
– Undoes all changes made during the current transaction, reverting the database to its state before the transaction began.SAVEPOINT
– Creates a point within a transaction to which you can later roll back, allowing you to undo only a portion of the transaction.SET TRANSACTION
– Sets properties for the current transaction, such as isolation level or access mode.visual summary and examples of each SQL command type:
Visual Summary Table
Category | Full Form | Purpose | Example Commands | Auto-Commit | Rollback Possible |
---|---|---|---|---|---|
DDL | Data Definition Language | Defines schema (structure) | CREATE , ALTER , DROP , TRUNCATE | ✅ Yes | ❌ No |
DML | Data Manipulation Language | Works with table data | SELECT , INSERT , UPDATE , DELETE | ❌ No | ✅ Yes |
DCL | Data Control Language | Controls access & permissions | GRANT , REVOKE | ✅ Yes | ❌ No |
TCL | Transaction Control Language | Manages transactions | COMMIT , ROLLBACK , SAVEPOINT | ❌ No | ✅ Yes |
Examples
DDL Example:
- CREATE TABLE Students ( StudentID INT, Name VARCHAR(100), Age INT );
- CREATE TABLE Students ( StudentID INT, Name VARCHAR(100), Age INT );
This command creates a new table named "Students" with three columns: "StudentID" (integer), "Name" (string), and "Age" (integer).
DML Example:
- INSERT INTO Students (StudentID, Name, Age) VALUES (1, 'Alice', 20); UPDATE Students SET Age = 21 WHERE StudentID = 1;
- DELETE FROM Students WHERE StudentID = 1;
- INSERT INTO Students (StudentID, Name, Age) VALUES (1, 'Alice', 20); UPDATE Students SET Age = 21 WHERE StudentID = 1;
- DELETE FROM Students WHERE StudentID = 1;
These commands demonstrate data manipulation: inserting a new student record, updating the age of a student, and deleting a student record.
DCL Example:
- GRANT SELECT, INSERT ON Students TO user1;
- REVOKE INSERT ON Students FROM user1;
- GRANT SELECT, INSERT ON Students TO user1;
- REVOKE INSERT ON Students FROM user1;
These commands control access to the "Students" table: granting a user named "user1" the ability to select and insert data, and then revoking the insert permission.
TCL Example:
- BEGIN TRANSACTION;
- INSERT INTO Students (StudentID, Name, Age) VALUES (2, 'Bob', 22);
- -- If no errors COMMIT;
- -- If there's an error -- ROLLBACK;
- BEGIN TRANSACTION;
- INSERT INTO Students (StudentID, Name, Age) VALUES (2, 'Bob', 22);
- -- If no errors COMMIT;
- -- If there's an error -- ROLLBACK;
Post Comment
Your email address will not be published. Required fields are marked *