Core CS · DBMS
Four families, and only one you can undo
DDL, DML, DCL and TCL are not four lists to memorise. They are four kinds of change, and which family a statement belongs to decides whether you can take it back.
Run a script and watch a rollback fail →01 The idea
Four kinds of change, not four lists to memorise
SQL is one language on paper. In practice a statement does one of four different kinds of work, and every textbook splits SQL into sub-languages by which one. CREATE TABLE invents a place to put rows. INSERT puts rows into it. GRANT decides who else may. COMMIT decides whether any of it survives. Four different targets: the structure, the data, the permissions, and the fate of the change.
The split earns its keep because of one consequence. The family decides whether you are allowed to change your mind. Run DELETE FROM student; inside an open transaction and you can still type ROLLBACK; and get every row back, because nothing was permanent yet. Run TRUNCATE TABLE student; and there is nothing left to roll back, because in Oracle and MySQL that statement committed your transaction before it did the work. Both statements emptied the same table. Only one of them is reversible, and the reason is the family it belongs to.
Where the DBMS writes the change down differs too, and it is the cleanest way to keep the families apart. DDL rewrites the data dictionary, the catalog tables from the first lesson that record which columns exist. DML rewrites the rows in the table’s own data pages. DCL rewrites the privilege catalog. TCL writes no new data at all; it decides what happens to writes that have already been made.
02 Worked example
The life of one table, in four families
One table and three students, carried through the whole lesson. The rows are the first three students from lesson one: Asha 78, Ravi 65, Meera 35. Follow the table from the moment it does not exist to the moment a change you thought was provisional turns out to be permanent.
| sid | name | marks |
|---|---|---|
S1 | Asha | 78 |
S2 | Ravi | 65 |
S3 | Meera | 35 |
Five steps built and changed that table, covering all four families, with DDL appearing twice. Section 04 steps through this same script with four more statements woven in. The order is what matters, and the highlighted step is where students lose marks.
The first four nodes behave the way their names suggest. The fifth is the one that catches people. ALTER TABLE student ADD email VARCHAR(40); looks like a change to the structure and nothing else. In Oracle and MySQL it fires an implicit COMMIT before it starts and another after it finishes. Meera’s 45, provisional a moment ago, is now permanent. A ROLLBACK typed on the next line reports no error and does nothing.
That behaviour is the reason to learn the four as families rather than as four lists of keywords. You are not memorising that ALTER is DDL for its own sake. You are memorising it so that when you meet it in the middle of a script you know your transaction has already ended, whether you wanted it to or not.
03 Mechanics
Which family, what it writes, and whether you can undo it
Four families, four different places the change is written down, and one column that decides whether you may change your mind. Read the last column first; that is the one interviews are testing.
| Family | Statements | What it changes | Where the DBMS writes it | Undone by a later ROLLBACK? |
|---|---|---|---|---|
| DDL Data Definition | CREATE, ALTER, DROP, TRUNCATE, RENAME | the structure: tables, columns, constraints, indexes | the data dictionary | No in Oracle and MySQL — it commits first |
| DML Data Manipulation | INSERT, UPDATE, DELETE | the rows inside a table | the table’s data pages, plus one undo record per row | Yes, until you COMMIT |
| DQL or DML, see below | SELECT | nothing at all | nothing | nothing to undo |
| DCL Data Control | GRANT, REVOKE | who may run which statement on which object | the privilege catalog | No in Oracle, which commits; yes in PostgreSQL |
| TCL Transaction Control | COMMIT, ROLLBACK, SAVEPOINT | which DML changes become permanent | the transaction’s own state | it is the undo |
DCL deserves one more sentence, because it is the family students describe most vaguely. A GRANT names an action, an object, and a grantee — and optionally a column list, so GRANT UPDATE (marks) ON student TO teacher; hands over one column and not the rest. It can also carry WITH GRANT OPTION, which lets the grantee pass the privilege on to somebody else. REVOKE takes a privilege back, and what happens to anyone the grantee passed it on to is where products differ: Oracle cascades the withdrawal to those onward grants for you, PostgreSQL refuses the REVOKE unless you write CASCADE, and MySQL does not cascade at all. Neither statement reads or writes a single row of data.
Now the trip-up. Two statements empty the same table, and every difference below follows from one of them being DML and the other being DDL.
| DELETE FROM student; | TRUNCATE TABLE student; | |
|---|---|---|
| Family | DML | DDL |
| How the rows go | one logged row deletion at a time | the table’s data pages are deallocated whole |
| WHERE clause | allowed | not allowed — every row or none |
| Row-level DELETE triggers | fire, once per row | do not fire |
| Cost on a million rows | grows with the row count | roughly constant |
| What ROLLBACK has to work with | one undo record per row | nothing per row was written |
| ROLLBACK on the next line | restores every row | Oracle, MySQL: already committed. PostgreSQL, SQL Server: works |
| Identity or AUTO_INCREMENT counter | keeps its current value | reset in MySQL and SQL Server |
| Privilege you need | DELETE on the table | a table-level right, not DELETE |
One more split, and this one sits inside DML rather than between families. It gets asked as “is SQL procedural or non-procedural?”, and the answer is a sentence rather than a word.
| Non-procedural DML | Procedural DML | |
|---|---|---|
| What you write | what you want | what you want, and the order in which to get it |
| Unit of work | a set of rows at once | one record at a time |
| Our example | UPDATE student SET marks = 45 WHERE sid = 'S3'; | open a cursor, FETCH each row, test it, update it, loop |
| Who picks the access path | the query optimiser | you do |
| Where you meet it | plain SQL, every day | PL/SQL and T-SQL blocks, cursors, SQL embedded in C, and the older CODASYL network-model DMLs |
| Cost of getting it wrong | a slow plan the optimiser may later fix | a loop nobody can optimise for you |
SQL is non-procedural DML. You describe the rows you want and the optimiser decides how to reach them — index or scan, which order, which join method. Procedural DML is what you fall back to when a set-based statement cannot express the logic, and it is slower for the same reason it is more flexible: you took the access path away from the optimiser and did it by hand.
Which leaves the question sources genuinely disagree on. Is SELECT part of DML, or a fifth family of its own called DQL?
The ISO SQL standard has no DQL. It groups SELECT, INSERT, UPDATE and DELETE together as SQL-data statements, kept separate from SQL-schema statements (the DDL) and SQL-transaction statements (the TCL). Many Indian university syllabi and placement question banks do list DQL as a fifth family, with SELECT as its only member. Both groupings follow from a definition of DML: one reads it as “statements that operate on the data”, which includes retrieval, and the other reads it as “statements that modify the data”, which does not.
05 Cheat sheet
Which statements survive a rollback
One row per statement you will be asked about, and the answer that gets marked. The vendor named in the last column is the exception worth saying out loud, because leaving it out is what invites the correction.
| Statement | Family | What it touches | Undone by a later ROLLBACK? |
|---|---|---|---|
CREATE, ALTER, DROP, RENAME | DDL | the data dictionary | No in Oracle and MySQL — it committed first |
TRUNCATE TABLE | DDL | data pages, deallocated whole | No in Oracle and MySQL; yes in PostgreSQL and SQL Server |
INSERT, UPDATE, DELETE | DML | rows, plus one undo record each | Yes, until you COMMIT |
SELECT | DML by the standard, DQL in many syllabi | nothing | nothing to undo |
GRANT, REVOKE | DCL | the privilege catalog | Oracle commits; PostgreSQL rolls it back |
COMMIT | TCL | ends the transaction | No — it is the point of no return |
SAVEPOINT sp1 | TCL | marks a point inside the transaction | changes no data |
ROLLBACK TO SAVEPOINT sp1 | TCL | undoes back to the mark; the transaction stays open | it is the undo |
ROLLBACK | TCL | undoes everything since the last commit | it is the undo |
06 Where & why
Where the rule holds, and where it does not
“DDL auto-commits” is a statement about specific products, not about SQL. It is true in the two databases most syllabi were written around and false in three others you have probably installed. Naming which is which is the difference between reciting a rule and understanding it.
Every DDL statement fires an implicit COMMIT before it runs and another after, and so does GRANT. This is the behaviour every “DDL cannot be rolled back” answer describes. Oracle softens the worst case with a recycle bin, so FLASHBACK TABLE ... TO BEFORE DROP can bring back a dropped table — but that is a recovery feature, not a rollback, and it will not undo a committed UPDATE.
The default session has autocommit = 1, so every single INSERT is its own committed transaction and ROLLBACK does nothing until you run START TRANSACTION or turn autocommit off. DDL causes an implicit commit on top of that. TRUNCATE also resets the AUTO_INCREMENT counter, which DELETE does not.
BEGIN; ALTER TABLE student ADD email ...; ROLLBACK; and the column is gone. TRUNCATE, CREATE TABLE and GRANT are all transactional here. This is exactly why migration tools like it: a migration that fails on step seven leaves the schema untouched instead of half-applied.
Everything, schema changes included, runs inside the same transaction, so a ROLLBACK undoes a CREATE TABLE. There is no GRANT at all: SQLite has no users and no privilege catalog, so the DCL family is absent altogether and the operating system’s file permissions do that job instead.
07 Interview questions
What they actually ask
This topic is asked early, because it is quick to mark and it separates people who have run SQL from people who have read about it. Expect a list question first, then one that turns on auto-commit. Answer each of these out loud in about twenty seconds.
What are the sub-languages of SQL?
Which statements are DDL, and what do they have in common?
Is SELECT DML or DQL?
Why is TRUNCATE classified as DDL and not DML?
Difference between DELETE, TRUNCATE and DROP?
Which statements can you not roll back?
What is a SAVEPOINT, and how does ROLLBACK TO SAVEPOINT differ from ROLLBACK?
Does COMMIT release savepoints?
I have a DDL statement in the middle of my script. What happens to the DML above it?
Procedural or non-procedural DML: which is SQL, and what is the difference?
What exactly do GRANT and REVOKE change?
When have you actually typed COMMIT in real code?
08 Practice problems
Six to work through
For every one of these, write down two things before you answer: which family each statement belongs to, and where the last commit happened. Almost every wrong answer on this topic comes from getting the second one wrong.