QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Database basics

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.

GroupCommandsActs on
DQL (queries)SELECTOnly reads
DML (data)INSERT, UPDATE, DELETE, MERGEChanges rows
DDL (structure)CREATE, ALTER, DROP, TRUNCATEChanges tables and schema
DCL (access)GRANT, REVOKEChanges permissions
TCL (transactions)BEGIN, COMMIT, ROLLBACKGroups 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

DQL: SELECTreads, changes nothingDML: INSERT UPDATE DELETEchanges rows, rollback until COMMITDDL: CREATE ALTER DROPchanges structure, lockingDCL: GRANT REVOKEchanges who can do whatTCL: BEGIN COMMIT ROLLBACKcontrols the change boundaries
Figure 1. Changes to structure or permissions have a larger impact on everyone using the database.

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

Risk

UPDATE without BEGIN

Autocommit: the change is committed at once. If the condition is wrong, the fix means restoring from a backup.

Safer

BEGIN, UPDATE, check, COMMIT

The "UPDATE 1" counter and a query after the change show what was touched. ROLLBACK removes the mistake.

Figure 2. Before running DML on a shared environment, check the condition with SELECT and run the change inside a transaction.

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 UPDATE or DELETE without WHERE and 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.

In an interview. Junior: Commands are divided into groups: SELECT for reading, INSERT, UPDATE and DELETE for changing data, CREATE, ALTER and DROP for structure, GRANT and REVOKE for permissions, BEGIN, COMMIT and ROLLBACK for transactions. I write SELECT most often and make data changes inside a transaction. Middle/Senior: The group hints at risk. DML rolls back until COMMIT, DDL can lock a table and does not roll back in MySQL and Oracle, while in PostgreSQL it does. On a shared environment I first check the condition with SELECT, look at the affected-row counter and, where possible, work under a read-only account.

More articles in this chapter