Skip to main content

Before you fire that delete command


Rule of thumb for me is that every DELETE or UPDATE query should start life as a SELECT query.


The eight stages of DELETE:

  1. SELECT what you're deleting;
  2. fix the mistake in the query (there's always one);
  3. SELECT again;
  4. BEGIN TRANSACTION;
  5. DELETE;
  6. SELECT to confirm the right deletion -- twice for good measure, and maybe ROLLBACK once or twice if you're feeling twitchy;
  7. Hover finger anxiously over the un-entered COMMIT command for several seconds while resisting Dunning–Kruger effect;
  8. COMMIT. :)

Or alternately:

  1. Give the table an DeletedTimestamp and DeleatedReason column.
  2. Mark the records as deleted.
  3. Run around like a lunatic and fix queries so they don't show "deleted" columns to the rest of the app. Rather create a "view" which never sends out "deleted" columns and rows.
  4. Make a page in the application where people can "restore" deleted items. Because they do stupid things too once in a while

Comments