DDL, DML, TCL, DQL

cheet sheet

Welcome to the SQL cheat sheet! This comprehensive guide covers essential SQL commands across different categories: DDL (Data Definition Language), DML (Data Manipulation Language), TCL (Transaction Control Language), and DQL (Data Query Language). Whether you are a beginner or an expert, this cheat sheet will help you quickly reference common SQL commands for database management and data manipulation.


1. DDL Commands (Data Definition Language)

DDL commands are used for defining and modifying database structures like tables, indexes, and schemas.

CommandDescriptionExample
CREATEUsed to create a new table, database, or other database objects.CREATE TABLE employees (id INT, name VARCHAR(50));
ALTERModifies an existing database object (e.g., table structure).ALTER TABLE employees ADD COLUMN age INT;
DROPDeletes an entire table or database.DROP TABLE employees;
TRUNCATERemoves all records from a table but retains the table structure.TRUNCATE TABLE employees;
RENAMERenames a table or database object.ALTER TABLE employees RENAME TO staff;

2. DML Commands (Data Manipulation Language)

DML commands are used for adding, updating, or deleting data in tables.

CommandDescriptionExample
INSERTAdds new rows of data into a table.INSERT INTO employees (id, name, age) VALUES (1, 'John', 30);
UPDATEModifies existing data in a table.UPDATE employees SET age = 35 WHERE id = 1;
DELETERemoves specific rows of data from a table.DELETE FROM employees WHERE age < 25;

3. TCL Commands (Transaction Control Language)

TCL commands manage transactions in SQL, helping to ensure data integrity.

CommandDescriptionExample
COMMITSaves the current transaction permanently.COMMIT;
ROLLBACKReverts the database to the last saved state.ROLLBACK;
SAVEPOINTSets a savepoint to mark a specific point in a transaction.SAVEPOINT sp1;
SET TRANSACTIONDefines the isolation level for transactions.SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

4. DQL Commands (Data Query Language)

DQL commands are used to query and retrieve data from a database.

CommandDescriptionExample
SELECTRetrieves data from one or more tables.SELECT * FROM employees WHERE age > 30;

Why This SQL Cheat Sheet is Useful

This SQL command reference guide is perfect for database administrators, data analysts, and anyone working with databases. Whether you’re managing databases, writing queries, or performing transactions, this cheat sheet is a valuable tool to quickly learn and implement SQL queries.