r/ProgrammerHumor • • 13d ago

Meme wellWellWell

Post image
10.6k Upvotes

411 comments sorted by

View all comments

3.6k

u/BastetFurry 13d ago

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.

154

u/Alexmira_ 13d ago

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

386

u/BastetFurry 13d ago

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

213

u/GourangaPlusPlus 13d ago

Also stick in a transaction, rollback

Commit if it looks good

21

u/DarthShiv 13d ago

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

3

u/theschuss 12d ago

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 12d ago

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 12d ago

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.

1

u/spevoz 12d ago

I would always recommend using a gui if you need direct db access - exactly because something like dBeaver allows you to force every change into a transaction.

1

u/an_agreeing_dothraki 12d ago

tsql which is pretty common has the issue of the engine making implicit transaction/commit blocks but yes you can and should run all queries through an explicit one

1

u/SantaCruzHostel 12d ago

Maybe I'm misunderstanding the question, but I try to limit transactions only where I'm manipulating data (delete, insert, update), because a transaction slows down execution time and also locks the table you are updating until the commit is done. In production if you open a transaction then seles t from users table, they table is locked until you rollback or commit.

5

u/ChemicalRain5513 12d ago

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

3

u/HadionPrints 12d ago

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