Safe UPDATE & DELETE Habits
Professional habits that stop you from accidentally changing or deleting the wrong rows — SELECT first, transactions, backups.
Every database professional has a story about the UPDATE that forgot its WHERE. The difference between a beginner and a pro isn't that the pro never makes mistakes — it's that the pro has habits that catch them before they hurt.
Definition
Safe data modification is a set of working habits for running
UPDATEandDELETEso that only the intended rows are affected, and so that any mistake can be detected and undone before it becomes permanent.
The golden rule from the course: ALWAYS use WHERE with UPDATE/DELETE, or EVERY row is changed.
Habit 1 — SELECT first, then change
Write the WHERE as a SELECT and look at the rows before changing them:
sql
-- Step 1: which rows will be affected?
SELECT *
FROM players
WHERE team = 'MI';| id | name | team | country | runs |
|---|---|---|---|---|
| 2 | Rohit Sharma | MI | India | 500 |
| 6 | Jasprit Bumrah | MI | India | 20 |
2 rows — exactly what we expected. Now reuse the same WHERE:
sql
-- Step 2: change exactly those rows
UPDATE players
SET runs = runs + 50
WHERE team = 'MI';Habit 2 — use a transaction as a safety net
A transaction is a group of statements that the database treats as one unit of work: either all of it is saved (COMMIT) or none of it is (ROLLBACK).
sql
BEGIN TRANSACTION;
UPDATE players
SET runs = 0; -- oops, forgot WHERE!
SELECT * FROM players; -- all zeros... (6 rows affected) was a warning sign
ROLLBACK; -- undo everything since BEGIN TRANSACTION
SELECT * FROM players; -- original runs are back| Command | Meaning |
|---|---|
BEGIN TRANSACTION | Start the safety net |
COMMIT | "Looks right — save it permanently" |
ROLLBACK | "Something's wrong — undo it all" |
Tip: Run the change, check the "rows affected" count and the data, and only then
COMMIT. Don't leave a transaction open for long — it locks the rows for other users.
Habit 3 — target rows by primary key
WHERE id = 4 changes exactly one row, because ids are unique. WHERE name = 'Dhoni' might match zero rows (the name is 'MS Dhoni') or several (two players with the same name).
Habit 4 — check "rows affected"
| You expected | SQL Server says | Meaning |
|---|---|---|
| 1 row | (1 row affected) | ✅ |
| 1 row | (6 rows affected) | ❌ WHERE missing or too broad — ROLLBACK! |
| 1 row | (0 rows affected) | ❌ WHERE matched nothing — check spelling/values |
In T-SQL scripts you can read the count with @@ROWCOUNT right after the statement.
Habit 5 — back up before big changes
Copy the table first (SELECT * INTO players_backup FROM players; in SQL Server), so you can restore any values you damage. In production, make sure a real database backup exists.
Habit 6 — beware "run selection"
In SSMS, if you highlight only part of a script and press F5, only the highlighted text runs. Highlighting just the first line of:
sql
DELETE FROM players
WHERE id = 6;runs DELETE FROM players — without the WHERE! Some developers keep a single-line habit for dangerous statements, or always type the WHERE before the SET/DELETE part.
Bonus — see what changed with OUTPUT (SQL Server)
sql
DELETE FROM players
OUTPUT deleted.id, deleted.name
WHERE runs < 100;The OUTPUT clause returns the rows that were deleted (or inserted.* / deleted.* for UPDATE), so you see exactly what happened.
Safe-change checklist
- ✅ Am I connected to the right database (test vs production)?
- ✅ Did I run the WHERE as a SELECT first?
- ✅ Am I inside
BEGIN TRANSACTION? - ✅ Does "rows affected" match what I expected?
- ✅ Only then:
COMMIT.
SQL Server vs MySQL / PostgreSQL
| SQL Server | MySQL | PostgreSQL | |
|---|---|---|---|
| Start transaction | BEGIN TRANSACTION (or BEGIN TRAN) | START TRANSACTION | BEGIN |
| Safety mode | — | SET SQL_SAFE_UPDATES = 1 (blocks UPDATE/DELETE without a key in WHERE; on by default in MySQL Workbench) | — |
| See changed rows | OUTPUT | — | RETURNING |
Common mistakes
- ❌ Testing changes directly on production data. ✅ Use a test copy first.
- ❌ Forgetting to
COMMIT— the change is lost when the session ends and rows stay locked meanwhile. ✅ Finish every transaction with COMMIT or ROLLBACK. - ❌ Writing a different WHERE in the UPDATE than the one you tested with SELECT. ✅ Copy–paste the exact WHERE.
Interview tip
Q: You accidentally ran an UPDATE without WHERE in production. What do you do?
A: If it was inside an open transaction, ROLLBACK immediately. If it was already committed, restore the affected values from a backup (or point-in-time restore of the database). Prevent it next time by running changes inside a transaction, testing the WHERE with SELECT, and checking the rows-affected count before COMMIT.
Key points to remember
- ALWAYS use WHERE with UPDATE and DELETE — otherwise every row is changed.
- Run the WHERE as a SELECT first to see exactly which rows will be affected.
- Wrap risky changes in BEGIN TRANSACTION … then COMMIT (save) or ROLLBACK (undo).
- Target rows by primary key (WHERE id = 4) for precise single-row changes.
- Check the '(n rows affected)' message — an unexpected number means ROLLBACK.
- Back up the table before big changes, and never test on production first.
- In SSMS, running only a highlighted part of a statement can drop its WHERE clause.
Try it yourself
SELECT first, then UPDATE with the same WHERE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
ROLLBACK saves you from a missing WHERE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
COMMIT when the change is correct
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
SQL Server: see deleted rows with OUTPUT
BEGIN TRANSACTION;
DELETE FROM players
OUTPUT deleted.id, deleted.name, deleted.runs
WHERE runs < 100;
PRINT @@ROWCOUNT; -- 1 (Jasprit Bumrah)
COMMIT;SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Practice: a safe DELETE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
