What is the difference between DDL and DML commands in SQL? Where do COMMIT and ROLLBACK fit?
ReportedSource: GeeksforGeeks interview experience: Cognizant GenC 2024
DDL (CREATE/ALTER/DROP/TRUNCATE) defines structure and often auto-commits; DML (INSERT/UPDATE/DELETE) changes data inside transactions; COMMIT makes changes permanent, ROLLBACK undoes uncommitted ones.
DDL (Data Definition Language) defines structure: CREATE, ALTER, DROP, TRUNCATE, RENAME. In many databases (Oracle, MySQL) DDL auto-commits, so it cannot be rolled back; PostgreSQL supports transactional DDL.
DML (Data Manipulation Language) changes data: INSERT, UPDATE, DELETE, MERGE (and SELECT, sometimes classified as DQL). DML runs inside transactions.
TCL (Transaction Control Language) manages those transactions:
COMMITmakes the transaction's changes permanent and visible to others.ROLLBACKundoes uncommitted changes.SAVEPOINTmarks a point to roll back to partially.
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT; -- or ROLLBACK if either update failed
Also: DCL (GRANT, REVOKE) controls permissions.
Why COMMIT matters. Until commit, other sessions do not see the changes (depending on isolation level) and a crash discards them.
See the concept: Join fan-out
| O101 | line 1 | ₹300 |
| O101 | P101 | FAILED |
| O101 | P102 | SUCCESS |
| line 1 | P101 | ₹300 |
| line 1 | P102 | ₹300 |
Wrong: the single ₹300 line is counted once per payment attempt.
Fix: aggregate payments to one row per order first
line 1, paid, ₹300, so SUM = ₹300