SQL: UPDATE and DELETE without WHERE hit every row

There is no confirmation prompt and no undo outside a transaction. The habit that prevents it is writing the statement as a SELECT first, then editing it in place.

Code
-- 1. See what you are about to change
SELECT * FROM users WHERE id = 42;

-- 2. Turn it into the UPDATE, keeping the same WHERE
UPDATE users SET email = 'new@x.com' WHERE id = 42;

-- Or make it recoverable:
BEGIN;
UPDATE users SET email = 'new@x.com' WHERE id = 42;
SELECT COUNT(*) FROM users WHERE email = 'new@x.com';   -- expect 1
COMMIT;   -- or ROLLBACK
Output
UPDATE 1        <- what you wanted
UPDATE 812445   <- the WHERE was missing

-- MySQL's client has a guard for exactly this:
--   mysql --safe-updates
-- It refuses an UPDATE or DELETE with no key in the WHERE clause.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25