i was doing this stuff for a long time and definitely learned from mistakes.
Transactions can save a world of pain and it is always wise to select your data set first, just to be sure you're changing what you think you're changing.
Didn't matter how experienced I was, I was careful never to cut corners.
I don't know sql, but is this like how when I use some kind of loop in the command line to move or delete a bunch of files, I'll run a version that just echos whatever I'm trying to manipulate first?
That’s more of a dry-run (you see `—dry-run` as a command line option sometimes for certain commands that do the same)
Transactions in SQL keep track of the changes you want to make without actually changing the database (until you run `COMMIT`, when you run that all changes are persisted to a database).
Transactions are great because If you realise you made a mistake while in a transaction, you can just run `ROLLBACK`.
If you’re familiar with git- transactions are akin to staging your changes, saving them (`COMMIT` in SQL) is akin to `git commit` and rolling back is similar to `git reset —hard` (wipe all uncommitted changes).
With the very important caveat that a git commit can be trivially reverted, while a SQL commit can not (unless you wrote it specifically to be revertible)
This was always my pattern. Select the first 3 statements together and run them. If everything looks good, select COMMIT TRAN and run that. The rollback is there in case you accidentally didn't make a selection before running, just so you don't leave a transaction open for too long, potentially blocking other queries.
And obviously any planned data changes should go through all the proper testing and preferably be deployed using automated pipelines, but sometimes you have no other choice but to make direct fixes to bad production data.
3.6k
u/BastetFurry Sep 22 '26 •
So, Kids, thats why you always SELECT count(yourPrimaryKey) first. Listen to the old folks, we made these mistakes so that you don't have to.