Modules / Database / DML Lab

DML in SQL

Data Manipulation Language changes what is in your tables. Run INSERT, UPDATE and DELETE against a live table, watch every affected row light up, and use a transaction to undo the damage.

inserted updated deleted

Overview

DML (Data Manipulation Language) includes statements that insert, update, and delete rows. INSERT adds data, UPDATE changes selected rows, and DELETE removes selected rows. SELECT is often taught alongside DML, though SQL classifications vary.

The commands that change rows

query.sqlSQLite
Result

The WHERE clause limits which rows change. Before running a broad update or delete, run an equivalent SELECT with the same predicate and inspect the rows. A missing WHERE can change or delete every row.

Make multi-step changes atomic

When several statements must succeed together, use a transaction: begin, perform the changes, then commit only when the required conditions hold. Roll back when any step fails. The exact syntax and error recovery behavior are engine-specific; see transactions and ACID.

For an operation that inserts a row or updates it on a key conflict, many engines provide an upsert syntax. Names and semantics differ (ON CONFLICT, ON DUPLICATE KEY, or MERGE), so consult the target engine. Make the conflict key explicit and consider whether a retry can duplicate an external side effect.

Bulk changes and safety

Large changes can hold locks, generate substantial logs, and affect replication. Batch them only when the batches preserve the intended transaction semantics. Record how many rows changed and verify important invariants afterward. Use bound parameters for values supplied by users; see SQL injection prevention.

Verify the scope of destructive statements

For a critical update, check both the predicate and the number of affected rows. An update expected to change exactly one account should fail safely if it changes zero or several rows. Use a transaction when the count must be checked before commit. For deletion, decide whether records should be physically removed or retained with a status and audit trail; soft deletion changes every query that must hide inactive rows. See WHERE for predicate behavior.

Use conditional updates for invariants

A read followed by a write can race when two clients act at once. For inventory, subtract one only where the current quantity is positive, then check that exactly one row changed. The database evaluates the predicate and update as one statement, avoiding an application-level gap between the check and the write. If several tables must change together, put those statements in a transaction. The correct isolation and locking strategy still depends on the database and the rest of the workflow.

Further reading

Predict, then reveal

About to run: set Raise salary by to its maximum (50). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “The commands that change rows”?

  2. What does this module say about “Make multi-step changes atomic”?

  3. What does this module say about “Bulk changes and safety”?

Cheat sheet

DML in SQL

Run INSERT, UPDATE, DELETE and SELECT on a live table and see why a missing WHERE clause can wipe every row in one statement.

DATABASE · vizlearn.in/database/dml_in_sql.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.