Pcornel
Databases · SQL Operations

INSERT, UPDATE and DELETE Safely

Cheat sheet · https://pcornel.dk/topic.php?topic=6166

Back to the Topic

Answer

Safe data changes: preview with SELECT, run inside a transaction, check affected rows, keep a backup or undo path, and batch large changes.

Remember

No WHERE = every row. Large deletes lock and bloat — batch them. Some clients auto-commit; disable or wrap in BEGIN.

First checks

  • SELECT count(*) FROM t WHERE <cond>; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Verify in this order

preview → transaction → change → verify → commit

Where it usually breaks

An UPDATE without WHERE changes every row.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6166

Confirmation

Confirm action

Continue?