DEV Community

Patrick Omondi Masese
Patrick Omondi Masese

Posted on

# DDL, DML, DCL & TCL: SQL's Building Blocks in Plain English

A practical, no-fluff breakdown of the four SQL command categories, with real examples for each.

What is DDL?

DDL, or Data Definition Language, is the set of SQL commands used to define, modify, and remove the structure of database objects such as tables, schemas, indexes, and views. Think of DDL as the architecture layer of a database. It doesn't touch the values inside a table; it builds and reshapes the container those values live in.

A key trait of DDL commands is that they are typically auto committed. Once a DDL statement runs, the change is applied immediately and permanently, so in most systems it can't be rolled back the way DML can.

Common DDL Commands

Command Purpose
CREATE Creates a new database object such as a table, view, or index
ALTER Modifies the structure of an existing object
DROP Permanently deletes an object
TRUNCATE Instantly removes all rows from a table while keeping its structure
RENAME Renames an existing object

What is DML?

DML, or Data Manipulation Language, is the set of SQL commands used to work with the data inside tables: inserting new records, updating existing ones, deleting rows, and retrieving results. If DDL builds the house, DML is everything that happens inside it.

Unlike DDL, DML operations are usually transactional. Changes can be committed to save them permanently, or rolled back to undo them, as long as the transaction hasn't been committed yet.

Common DML Commands

Command Purpose
SELECT Retrieves data from one or more tables
INSERT Adds new rows to a table
UPDATE Modifies existing rows
DELETE Removes rows that match a condition

DDL in Action

Creating a table lays out the shape data will take, column by column.

CREATE TABLE customer (
    customer_id INT PRIMARY KEY,
    item_purchased VARCHAR(100),
    category VARCHAR(50),
    discount_applied VARCHAR(3),
    purchase_frequency_days INT
);
Enter fullscreen mode Exit fullscreen mode

Altering a table changes its shape after the fact, without touching existing rows.

ALTER TABLE customer
ADD email VARCHAR(255);
Enter fullscreen mode Exit fullscreen mode

Dropping a table removes it, and its structure, for good.

DROP TABLE customer;
Enter fullscreen mode Exit fullscreen mode

Truncating clears every row instantly, while leaving the table itself in place.

TRUNCATE TABLE customer;
Enter fullscreen mode Exit fullscreen mode

Notice the pattern: every DDL statement above talks about the table itself, never about a specific row of data.

DML in Action

Inserting adds a new row to the table just created.

INSERT INTO customer (customer_id, item_purchased, category, discount_applied, purchase_frequency_days)
VALUES (1, 'Running Shoes', 'Footwear', 'Yes', 14);
Enter fullscreen mode Exit fullscreen mode

Updating changes a value in an existing row.

UPDATE customer
SET discount_applied = 'No' WHERE customer_id = 1;
Enter fullscreen mode Exit fullscreen mode

Deleting removes rows that match a condition, and nothing else.

DELETE FROM customer
WHERE purchase_frequency_days > 90;
Enter fullscreen mode Exit fullscreen mode

Selecting reads data back out, without changing anything.

SELECT item_purchased, category, discount_applied
FROM customer
WHERE discount_applied = 'Yes';
Enter fullscreen mode Exit fullscreen mode

What is DCL?

DCL, or Data Control Language, is the set of SQL commands used to control who can access or modify data. Where DDL shapes the tables and DML works the data inside them, DCL decides who is allowed to touch either one. It lives at the permissions layer of a database.

Common DCL Commands

Command Purpose
GRANT Gives a user or role specific privileges on an object
REVOKE Removes privileges previously granted

What is TCL?

TCL, or Transaction Control Language, is the set of SQL commands used to manage transactions: groups of DML statements that should succeed or fail together. TCL is what makes it possible to change several rows as one unit, and undo the whole thing if something goes wrong partway through.

Common TCL Commands

Command Purpose
COMMIT Permanently saves all changes made in the current transaction
ROLLBACK Undoes changes made in the current transaction
SAVEPOINT Marks a point within a transaction to roll back to later
SET TRANSACTION Sets properties for the current transaction, such as isolation level

DCL & TCL in Action

Granting a privilege lets a specific user query the table.

GRANT SELECT, INSERT ON customer TO analyst_role;
Enter fullscreen mode Exit fullscreen mode

Revoking takes that privilege back.

REVOKE INSERT ON customer FROM analyst_role;
Enter fullscreen mode Exit fullscreen mode

A transaction groups several DML statements so they commit, or roll back, together.

BEGIN;

UPDATE customer SET discount_applied = 'No' WHERE customer_id = 1;
SAVEPOINT before_delete;
DELETE FROM customer WHERE purchase_frequency_days > 90;

COMMIT; -- or ROLLBACK TO before_delete;
Enter fullscreen mode Exit fullscreen mode

DDL and DML change what exists and what it holds. DCL decides who's allowed to do that. TCL decides whether the changes actually stick.

The Simple Way to Remember It

DDL defines the structure: tables, columns, and keys, and its changes usually take effect immediately. DML manipulates the data itself: the rows and values inside that structure, and those changes can typically be rolled back before they're committed. DCL controls who has access to do any of it. TCL controls whether a group of changes becomes permanent or gets undone.

If you're changing what a table looks like, that's DDL. Changing what's stored inside it, that's DML. Deciding who can do either, that's DCL. Deciding whether a batch of changes sticks, that's TCL.

Four categories, one mental model: define it, manipulate it, control access to it, commit to it.

Top comments (0)