Which groups are SQL commands divided into?
SQL commands are divided into groups by what they act on: data, structure, permissions or transactions. DQL (SELECT) only reads, DML (INSERT, UPDATE, DELETE) changes rows, DDL (CREATE, ALTER, DROP) changes structure, DCL permissions, TCL transactions. The group hints at risk: DML can be rolled back before COMMIT, while DDL can be rolled back in PostgreSQL but not in MySQL or Oracle.
SQL · Basics
A colleague asks you to "just run a script" on the environment. Does it read data, change data or change structure?
The environment script contains SELECT, UPDATE, ALTER and GRANT. Some lines are safe, others change data, still others can lock a table or hand out permissions. SQL commands are split into groups not for the sake of terms: the group tells what exactly changes, whether it can be rolled back and how careful you must be. This lesson goes through the groups and how to work with them in a test database.
How it works and why. The groups correspond to what the command acts on: rows, structure, permissions or a transaction.
| Group | Commands | Acts on |
|---|---|---|
| DQL (queries) | SELECT | Only reads |
| DML (data) | INSERT, UPDATE, DELETE, MERGE | Changes rows |
| DDL (structure) | CREATE, ALTER, DROP, TRUNCATE | Changes tables and schema |
| DCL (access) | GRANT, REVOKE | Changes permissions |
| TCL (transactions) | BEGIN, COMMIT, ROLLBACK | Groups changes and ends them |
The split is conventional: SELECT is sometimes counted as DML, and TRUNCATE is often called DDL although it removes data. Both variants occur, so in an interview it is best to name the group and explain what it does.
What each group changes
Example: from "just an UPDATE" to a safe change. Preparing test data: one account balance must be zeroed. Autocommit is on by default in most clients, so each command is committed at once:
UPDATE accounts SET balance = 0;
-- UPDATE 3: without WHERE every row changed, no way back
The forgotten WHERE changed every row, and nothing can be rolled back because the command has finished. The safer order is: open a transaction, change, check the number of rows affected and the result, and only then commit:
BEGIN;
UPDATE accounts SET balance = 0 WHERE id = 10;
-- UPDATE 1: one row, as expected
SELECT id, balance FROM accounts WHERE id = 10;
ROLLBACK; -- or COMMIT when the result is right
Changing data on a shared environment
UPDATE without BEGIN
Autocommit: the change is committed at once. If the condition is wrong, the fix means restoring from a backup.
BEGIN, UPDATE, check, COMMIT
The "UPDATE 1" counter and a query after the change show what was touched. ROLLBACK removes the mistake.
Limits and dialects. Rolling back DDL depends on the DBMS. In PostgreSQL ALTER, CREATE and DROP can be rolled back inside a transaction:
BEGIN;
ALTER TABLE accounts ADD COLUMN note text;
ROLLBACK;
-- PostgreSQL: no note column, the DDL rolled back too
In MySQL and Oracle DDL ends the current transaction implicitly (implicit commit), so a rollback after it is impossible. TRUNCATE in PostgreSQL is transactional too, but it takes a strong table lock and does not fire row triggers; in MySQL it is DDL with an implicit commit. Permissions (DCL) help QA: for checks it is convenient to have a separate account with SELECT only, then an accidental change is impossible. QA rarely writes GRANT and REVOKE, but understands why "permission denied" on an environment is not a bug.
Common mistakes.
- Running
UPDATEorDELETEwithoutWHEREand without a transaction. - Assuming DDL can always be rolled back: in MySQL and Oracle it cannot.
- Mixing up DELETE and TRUNCATE: different locking, rollback, counters and triggers.
- Working on an environment under an account with full rights although reading is enough for checks.
- Forgetting to check the affected-row counter: "UPDATE 3" where one was expected.
Check yourself
How does DML differ from DDL and why does it matter when choosing actions on a shared environment?
Answer
DML changes rows (INSERT, UPDATE, DELETE) and can be rolled back until COMMIT. DDL changes structure (ALTER, DROP) and may lock a table, and in some DBMS it cannot be rolled back. So DDL on a shared environment is run only by agreement.
After an UPDATE the client shows "UPDATE 3" instead of the expected "UPDATE 1". What to do if the transaction is not finished yet?
Answer
Run ROLLBACK, check the WHERE condition with a SELECT and repeat the change. The affected-row counter is the first signal that the condition is wrong.