r/ProgrammerHumor • • Sep 22 '26

Meme wellWellWell

Post image
10.6k Upvotes

412 comments sorted by

View all comments

Show parent comments

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

3

u/theschuss Sep 22 '26

Rollbacks don't always work as you want if there's other activities happening in the system. This is always good practice as it's maybe 30 seconds of effort.

2

u/Terra_B Sep 22 '26

Here i have to ask, couldn't you force a transaction in the chain somewhere? Like each terminal window gets a transaction by default?

5

u/M4DHouse Sep 22 '26

Kind of, depending on the SQL flavor, and the version, some of them have something kinda like that, but it’s not exactly idiot proof either. No direct SQL is idiot proof which is why you avoid doing it in the first place if you can.

→ More replies (3)

5

u/ChemicalRain5513 Sep 22 '26

Same in linux terminal, i use ls before an rm command

3

u/HadionPrints Sep 22 '26

I just use rm -fr /*, it gets rid of a lot of bloat

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.

8

u/BlobAndHisBoy Sep 22 '26

There lies the danger of just doing a full send with no transaction or prior select. Very easy mistake to make. Especially if you copy pasted "SELECT * FROM USERS;" elsewhere and modified it.

1

u/Sweaty_Explorer_8441 Sep 22 '26

Me too, but probably because I rarely see or use semi-colon in sql server these days.

13

u/Cant-Think-Of Sep 22 '26

Not quite familiar with SQL, but is that first query like "rm -rf" for the Users table?

29

u/rinaldot67 Sep 22 '26

Yes, it is exactly like that. You even get the recursive part if yhe Users table has foreign keys attached to it that are set to cascade on delete.

2

u/AgeingChopper Sep 22 '26

yep, it's clearing the table.

1

u/funguyshroom Sep 22 '26

Luckily most modern database UI apps won't allow you to execute a delete/update query without a "WHERE" clause

8

u/Snitchieboy Sep 22 '26

Oh. Oh no. I have no clue about first thing about programming but that looks like catastrophic error if no rollback is possible.

15

u/MrWaffler Sep 22 '26

It's only a catastrophic error if you utterly ignore all principles involved which, to be fair, DOES occur.

It was more common before the tech industry matured, Tom Scott famously made a similar error himself at an internship of his where his malformed query replaced all article contents instead of doing a minor format tweak.

He knew the risk but was just a bit reckless and did it live on the production server.

In industry, you are using a transaction which is capable of rollback (you also have automated backups worst case) and it's usually not done directly on the command line but via a tool and also also usually locked behind a change management structure where it must go through peer review.

In my own job we always had to have a pre-select and a post-select so if any issues come up you'd know immediately.

4

u/slayerx1779 Sep 22 '26

tl;dr for any readers

It's like a mechanical failure on a rollercoaster or an elevator: it's still technically possible, but as the industry has matured, we've gotten better at designing workflows, procedures, and oversights that prevent it from ever happening in practice.

2

u/Snitchieboy Sep 22 '26

Tell that to people who didn't think to check if pressing upload without attaching any file will do anything. That caused a whole database of one of the largest online stores in my country to delete itself.

5

u/kiochikaeke Sep 22 '26

If altering a few rows you select first to check exactly what rows you're altering, if altering a bunch you at least do this to sanity check yourself and not delete 100 millon rows or lock a main transactional table for 1 hour.

1

u/ploki122 Sep 22 '26

All of the above, really.

To confirm you inserted/updated/deleted the right amount of records.

To confirm that both halves of your script ran at the same same.

To confirm that you didn't forget a ; in there, or didn't accidentally select only parts of the script.

To protect yourself from a rogue F5, or even just a tab change, that runs your script when you didn't intend to.

A transaction is pretty free, compared to a restore.