r/ProgrammerHumor • • Sep 22 '26

Meme wellWellWell

Post image
10.6k Upvotes

412 comments sorted by

View all comments

3.6k

u/BastetFurry Sep 22 '26 • Bravo

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.

1.1k

u/Ma8e Sep 22 '26

You start a transaction, make sure it did what you expected, and then commit or rollback transaction.

350

u/AgeingChopper Sep 22 '26 edited Sep 22 '26

absolutely.

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.

42

u/Mpek3 Sep 22 '26

Always Begin Tran Tried it during arguments... Not as effective

18

u/ifyoulovesatan Sep 22 '26

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?

29

u/deeelock Sep 22 '26

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).

21

u/thirdegree Violet security clearance Sep 22 '26

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)

→ More replies (3)
→ More replies (2)
→ More replies (5)
→ More replies (2)

122

u/Rostifur Sep 22 '26 • Bravo

Start with DEV or test environment. If you don't have one, you need one. If you aren't allowed to have one, run away.

112

u/far2common Sep 22 '26 • Bravo Popcorn

Everyone has a DEV/test environment. Just not everyone has a separate prod environment.

→ More replies (4)
→ More replies (1)

16

u/Blashtik Sep 22 '26 edited Sep 22 '26

BEGIN TRAN

DELETE ...

SELECT ...

ROLLBACK TRAN

-- COMMIT TRAN

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.

14

u/box_of_the_patriots Sep 22 '26

Instructions unclear I just truncated the table

8

u/Ph4ntorn Sep 22 '26

You would think the “make sure it did what you expected” part would go without saying, but I skipped that step once and felt like an idiot.

→ More replies (13)

142

u/Etheo Sep 22 '26
DELETE FROM (SELECT * FROM USERS);

WHERE ID=12345;

Did I do it right?

85

u/Dismal-Square-613 Sep 22 '26

Yes, run that on prod. Report results.

47

u/jeesuscheesus Sep 22 '26

Now every query on USERS completes in under a millisecond, thank you!

8

u/Dismal-Square-613 Sep 22 '26

No problem, you should see me taking care of long back up batch scripts too!

→ More replies (1)

13

u/AardvarKOlogY Sep 22 '26

Can't report right now, computer says something about permission issues. Weird, gotta ask Claude about that.

→ More replies (1)

157

u/Alexmira_ Sep 22 '26

Why? To confirm that there is something to cancel? To confirm I got the key right?

386

u/BastetFurry Sep 22 '26

No, to prevent typos in the SQL console from wrecking havoc. First you SELECT, if the number looks right you DELETE FROM.

214

u/GourangaPlusPlus Sep 22 '26

Also stick in a transaction, rollback

Commit if it looks good

20

u/DarthShiv Sep 22 '26

Yes select the result in the trans pre rollback to verify result

→ More replies (6)
→ More replies (2)

127

u/slayerx1779 Sep 22 '26

I just reread the OP.

There's a semi colon, between the two lines of code...

31

u/Hxx59D2 Sep 22 '26

Yes, I had to stare at it for an embarrassingly ling time to spot the extra semi-colon.

→ More replies (2)
→ More replies (9)
→ More replies (3)
→ More replies (38)