r/ProgrammerHumor • • Oct 14 '25

Advanced neverForget

Post image
14k Upvotes

616 comments sorted by

4.2k

u/Spillz-2011 Oct 14 '25

If there’s no danger how do you get the rush. Don’t tell me you use transactions.

1.3k

u/BoBSMITHtheBR Oct 14 '25

What’s the fun if you use Transactions? Might as well wear a seat belt.

447

u/Spillz-2011 Oct 14 '25

Do you know some people look both ways before crossing the street? Sky diving costs money anyone can get that rush for free.

171

u/[deleted] Oct 14 '25

Jump without a chute and you'll be skydiving for the rest of your life.

43

u/[deleted] Oct 14 '25

They say he died doing what he loved… having his organs blended together and mixed with his bones.

→ More replies (1)

70

u/marcodave Oct 14 '25

If you haven't died at least once you haven't even lived

→ More replies (1)

9

u/mickaelbneron Oct 14 '25

You should cross a busy street in Vietnam someday. You'll feel that rush. You can Google videos of it if you want a preview.

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

152

u/HildartheDorf Oct 14 '25

I use transactions.

You write begin transaction

You write commit

Then you go up and write the update/delete.

93

u/CharlieKiloAU Oct 14 '25

You comment out the COMMIT though, right.... right?

50

u/Forzyr Oct 14 '25

Anakin stare

20

u/Rare_Ad_649 Oct 14 '25

I put a rollback, the change it to a commit later

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

13

u/code_monkey_001 Oct 14 '25

I just drop in the following snippet
--DELETE FROM
SELECT TOP 10 * FROM

WHERE

that way whatever I type into the gap as the table name will throw an error until I complete the WHERE, and even then will just give me rows validating the WHERE logic until I swap the commenting between the first and second lines.

3

u/realzequel Oct 14 '25

So I start ad-hoc commands with:

BEGIN TRANSACTION

[SPACE FOR COMMAND....]

ROLLBACK TRANSACTION

COMMIT TRANSACTION

If It runs by accident after I write the command, it'll rollback then the commit will throw an error which is fine.

When I'm ready to run, I'll highlight (in SQL Studio, you can highlight the part you'd like to run) the BEIGN TRANSACTION and the command. If I like the results I'll highlight and run the commit otherwise the highlight and run the commit.

→ More replies (1)

61

u/5t4t35 Oct 14 '25

Ye only pussies use transaction fuck it run it on the production server just to see if it works live

28

u/GraciaEtScientia Oct 14 '25

And if it doesn't work call it an "Interesting data point" but that more "data points" will need to be gathered to make any real determination of who is really to blame for prod being down now.

18

u/GeneralQuinky Oct 14 '25

Yeah just call up the DB guy afterwards and say "hey you guys have backups right"

12

u/5t4t35 Oct 14 '25

They say yes and shows you a .docx file in microsoft word

28

u/anomalous_cowherd Oct 14 '25

db_backup.sql 0 bytes 1 Jan 2018

→ More replies (2)

6

u/tehfrod Oct 14 '25

Did that at my first real job. I was "too busy" to learn the company's... what did they call it? "source control system"? after we were acquired.

After all, I keep a copy of the source code on a mapped server drive. It's perfectly safe.

blows away local copy before copying down from server

copy from server fails

Weird. I guess I wasn't in the local source directory? Oh well.

Changes directory, blows away local copy before copying down from server

realizes that step 1 occurred on the mapped server drive

"Uh, hi, is this IT? Ok... Just wondering, do y'all happen to run nightly backups on the file server?"

→ More replies (2)

19

u/catom3 Oct 14 '25

I remember working on Oracle years ago. And we had pleeeenty of triggers on tables. We had a simple task to update one record, which was not updated due to the logic error. We also didn't want any DB trigger to run when performing that update.

So... The dev prepared a standard anonymous PL/SQL block with commands like BEGIN DISABLE ALL TRIGGERS; UPDATE foo SET bar = 'dummy'; ENABLE ALL TRIGGERS; END

The dev opened a transaction and ran it, just to test it. The dev noticed their missing WHERE clause and rollbacked the transaction.

Ooppps. All records changed their bar column to value from this update. Wait? Why?

Ohhhhh... Oracle's DISABLE/ENABLE TRIGGERS statement is not really transactional and always makes an implicit commiy for you.

Of course, I don't want to be dismissive and I agree with you. Just that running everything within a transaction isn't a silver bullet either.

Worth stating that the application design was definitely not helpful. Neither were the practices of testing such SQLs on a real, production, live database. :)

4

u/TastesLikeTesticles Oct 15 '25

I remember working on Oracle

see, there's your issue

→ More replies (1)

3

u/weezeelee Oct 15 '25

DDL statements that modify global metadata inside transactions are a big no no.

But surprisingly, not a lot of SQL "best practices" mention this (because they all use Postgres I guess?)

So 99% of non-Postgres developers learn this through an accidental table drop.

8

u/markuspeloquin Oct 14 '25

Transactions will lock some rows until you commit. That's a non-starter if you're typing commands into a production database. Be smart and don't use transactions. /s but also kinda not

I guess the right answer is to put it in a text file; start a transaction, do the thing, and abort. Make sure it looks right. Then switch the abort to commit and rerun it. Maybe.

18

u/Kellei2983 Oct 14 '25

the right answer is to let a colleague ruin his day instead

3

u/[deleted] Oct 14 '25

Everyone knows the rush only happens after you actually delete the whole production table

2

u/brekekekekx Oct 14 '25

Using transactions means doubting your own SQL skills. It's a sign of weakness.

→ More replies (1)
→ More replies (7)

1.7k

u/usrlibshare Oct 14 '25

Bet SQL dialects that enforce the closing semicolon lookin pretty good right now 😎

185

u/markuspeloquin Oct 14 '25

Does anything not require semicolons?

347

u/usrlibshare Oct 14 '25

Strictly speaking, most SQL dialects require it.

However: many SQL workbenches (editors, environments) insert the ; for the user, because apparently typing an extra character to unambiguously signalling an end of statement is a lot of work.

Which sounds awesome, right until people discover, that some prefixes of statements, like DELETE FROM table are also valid statements in themselves, and that accidentally touching the ENTER key is a thing 😎

Less strictly speaking, since many SQL dialects are closely associated with particular workbenches, drivers, odbc connectors, etc. the requirement or lack thereof to type the semicolon is almost a part of the dialect.

67

u/Blue_Moon_Lake Oct 14 '25

Even with a WHERE clause, you maybe be missing an AND x=y and delete unintended rows.

28

u/nicuramar Oct 14 '25

 Strictly speaking, most SQL dialects require it

Only to separate statements, like in Pascal. Not to terminate them. 

14

u/FreakDC Oct 14 '25

Which IDE sends queries on enter? Any that I have used just create a new line...

→ More replies (4)
→ More replies (9)

14

u/CurryMustard Oct 14 '25

T-sql doesn't

12

u/nicuramar Oct 14 '25

Most don’t. 

8

u/BolaSquirrel Oct 14 '25

SSMS will run a statement with or without it

→ More replies (7)

32

u/Hampster-cat Oct 14 '25

This needs to be the top response.

→ More replies (4)

2.2k

u/Ghostserver10 Oct 14 '25

I usually never type delete or update. Select first, see what you're about to change only then 

772

u/Hatchie_47 Oct 14 '25

Exactly this, you never wanna run delete or even update without checking the results first - at least on data that matters.

3

u/backwardcircle Oct 14 '25

OR, do it inside a transaction. Open transaction, do random shit, validate. If okay comnit, else rollback.

→ More replies (16)

63

u/Titaniumwo1f Oct 14 '25

I always wrap any data modification statement in transaction though, and it always end with rollback unless I really need to commit.

19

u/Brendoshi Oct 14 '25

I do:

Select

Transaction

delete

--rollback

--commit

select

Gives me the data before, the data after (so I can see the changes I've made), and I'll also check the changed rows in case I've been dumb and forgot to account for triggers, and make sure those are all correct.

If I'm happy that the result has done what I want, commit. If I'm unhappy, rollback and rework my statements

57

u/[deleted] Oct 14 '25

python dev here, i just fuckin

import tables

tables = None
→ More replies (2)

11

u/BroBroMate Oct 14 '25
DELETE FROM X WHERE PK IN (
   SELECT PK FROM X WHERE VERY FUCKING SPECIFIC CLAUSE)

And of course you run the select first. Repeatedly. To be sure.

7

u/gitpullorigin Oct 14 '25

Just don’t press Enter before you typed that WHERE clause

→ More replies (16)

3

u/LGHTHD Oct 14 '25

Kinda just proving his point eh

2

u/codeptualize Oct 14 '25

I use multiple measures:

  • Don't run queries on prod unless you have no other options
  • Indeed first do select
  • Always test first in a transaction with rollback, check the counts, only when super certain change rollback to commit
  • Have your db client make you confirm write actions on prod environments, I use TablePlus, makes me touch id before I can run write commands on prod.
begin;
-- typing happens here
rollback;
→ More replies (2)

4

u/liljoey300 Oct 14 '25

How does doing a select statement first change this?

45

u/smors Oct 14 '25
  1. select j from jokes where j.quality = 'boring'
  2. validate that the jokes are indeed boring.
  3. change select to delete.

At no point in this process is there a 'delete j from jokes' whitout the where clause.

→ More replies (4)

11

u/Christoxz Oct 14 '25

Because then you have you query properly prepared with a 'where' statement, and will not run accidently a delete query without where statement.

→ More replies (18)

200

u/MrHall Oct 14 '25

many years ago i changed SQL client to one that would helpfully just run the query or partial query you have highlighted. the previous client didn't do that and i had no idea it was a feature.

I had a very, very important data fix to update the state of a particular user who had been put into the wrong state by a bug in a long and complex user workflow.

i typed (the state was an enum):

UPDATE user_state SET current_state = 42 WHERE user_id = 7A624CEC-91C6-4444-A798-EA9622CE037F;

i ran a query on the user table with that ID to absolutely ensure the correct user was being reset, i highlighted the WHERE condition and re-read it twice to be sure, i highlighted the UPDATE/SET part of the query and re-read it to be certain i was setting the right thing in the right table, and I hit run.

and it ran the update without the condition, which reset the state for every single user in the entire system, in production, on a critical workflow that would take users weeks, that users had been actively working away in all day, with backups only happening overnight.

lessons were learned that day.

before anyone chips in that was maybe 20 years ago and I know absolutely everything i could have done to prevent that from happening now.

63

u/mbriedis Oct 14 '25

That's such crazy UX. Imagine as soon as you put your butt in the cars seat it immediately starts driving.Who thought that's a great idea. For Select maybe, but still

18

u/fish312 Oct 14 '25

Select * from a 1000 column hundred million row table

8

u/MrHall Oct 14 '25

it was Microsoft SQL server management studio - i wonder if it still does it? Ai reckons that's still how it works but who knows

6

u/AzazelsAdvocate Oct 14 '25

Yes, SSMS still does this

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

23

u/otrippinz Oct 14 '25

Rollback

55

u/mbriedis Oct 14 '25

Roll back what? A transaction that didn't exist?

→ More replies (10)
→ More replies (3)

8

u/KontoOficjalneMR Oct 14 '25

"and it ran the update without the condition"

how?

44

u/teddy5 Oct 14 '25

They highlighted the UPDATE SET part of the statement without the WHERE, not knowing that would make the client only execute the highlighted portion of the query.

11

u/KessieHeldieheren Oct 14 '25

Holy shit lmao

2

u/user_8804 Oct 15 '25

That happened to me on SSMS too. Luckily it wasn't a big deal to fix in my case. I've been paranoid about it ever since though. I had made a small app to inject my queries instead of running them in SSMS so it wouldn't play tricks on me. Idk if it still works like that.

3

u/Vladutz19 Oct 14 '25

Won't that crash, because you didn't enclose the ID in quotes?

3

u/teddy5 Oct 14 '25

Depends on the system, some have a guid datatype which may not need to be quoted

→ More replies (1)
→ More replies (5)

87

u/DarkLordTofer Oct 14 '25

1,237,836,384,823 lines affected

63

u/obsoleteconsole Oct 14 '25

Update query runs for more than 2 seconds

16

u/xenopunk Oct 14 '25

I have genuinely had this happen, and thankfully caught it in time before it changed everything. Purely because I realised it was taking too long.

Learn some lessons the hard way.

→ More replies (1)

5

u/masd_reddit Oct 15 '25

The onosecond

2

u/slgray16 Oct 15 '25

Literally happened to my coworker. Had the proper command vetted but somehow highlighted everything before the where clause.

Entire customer database had to be rebuilt from a backup 4 hours old. Enterprise customer. Team was busy that week.

72

u/SeriousPlankton2000 Oct 14 '25

Just don't commit the transaction. You did start a transaction, didn't you? Also you were on the test database, right?

38

u/imverynewtothisthing Oct 14 '25

Right?

37

u/NeinJuanJuan Oct 14 '25

"Psshht. Yes. Definitely. Of course it was the test database. 

One question though: hypothetically.. I mean, like academically speaking.. what would happen if it wasn't the test database? 👉👈"

26

u/gnutrino Oct 14 '25

Also you were on the test database, right?

In the "everyone has a test environment, some lucky people also have a separate prod environment" sense - technically, yes.

→ More replies (1)

2

u/raskinimiugovor Oct 14 '25

Then leave the computer on when you go home, blocking the table for hours/days.

→ More replies (1)
→ More replies (3)

164

u/zuzmuz Oct 14 '25

sql has the worst syntax for real. everything in reversed. it should've been

FROM table WHERE condition SELECT columns.

it makes more sense and you can have intelisense autocompletion on the column names. this way the editor can help you browse the column names and you wouldn't have a typo.

Same with delete. you start with the table name, condition, then the final statement, which is either select delete or update.

9

u/ChewiesHairbrush Oct 14 '25

Auto complete! SQL was specified in a time when teletypes and punch cards predominated. 

Kids!

9

u/zuzmuz Oct 14 '25

exactly, that's not a good argument. I just gave one example why the reverse order is better.

There's so many.

  • if you give aliases to tables, you'll be using them before defining theme, you'll have to do backtracking while reading especially complicated queries.
  • using complicated features like pivot would look saner. select should comes after the pivot. right now you select the pivoted columns first before defining them, this is crazy actually.
  • there's a lot of other reasons, but finally, it would mimic how we think, take a table, filter it, select what you want from it. it’s sequential, linear, and makes more sense, and would require less backtracking
→ More replies (2)

5

u/sndrtj Oct 14 '25

You can do

SELECT tablename.colname, tablename.colname2 from tablename where condition

This gives you autocomplete on the column names.

25

u/zuzmuz Oct 14 '25

yes, and redundancy.

Sql was designed to be readable in a way that 'non technical' people could read it and write it.

that's always a bad idea. look at cobol.

flipping the order of statements would make everything clearer, i just gave one example. but select coming after group by for example would make much more sense.

queries will be written as data manipulation process and will be linear and easier to reason with, so complicated queries are easier to write and read. You start with the raw data and filter/process it till you get what you need. it's objectively better

→ More replies (3)
→ More replies (7)

170

u/Objectionne Oct 14 '25

Don't most modern database engines require a condition when deleting these days?

303

u/[deleted] Oct 14 '25

HA!
who has a modern db? That requires upgrades n stuff and if it aint broke, dont touch it bc it will all shatter at the abstracted notion of the lightest breeze

31

u/TRKlausss Oct 14 '25

But like, not having a condition when deleting is being broken…

36

u/amzwC137 Oct 14 '25

Guardrails schmard rails, who needs 'em.

→ More replies (3)
→ More replies (3)

52

u/prehensilemullet Oct 14 '25

Postgres does not

But in any case psql requires a semicolon

18

u/VolcanicBear Oct 14 '25

And any sane person is beginning and ending transactions.

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

24

u/nonlogin Oct 14 '25

Some clients do, not db engines

13

u/thebeerhugger Oct 14 '25

WHERE 1 = 1

8

u/freeflow276 Oct 14 '25

You cannot save them all

→ More replies (1)

3

u/Jason1143 Oct 14 '25

That's fine. Because typing that shows intent. The issue isn't being able to nuke everything, the issue is being able to do it by accident.

24

u/JiminP Oct 14 '25

SQLite doesn't.

On one hand, using SQLite in production is weird.

On the other hand, it might not be that weird.

On the other other hand, it still feels weird.

18

u/leaningtoweravenger Oct 14 '25

SQLite in production is ok only as a disk storage for a local app when you don't want to use files on disk manually

10

u/JiminP Oct 14 '25

ok only as a disk storage for a local app

SQLite in production for an online service like a webapp is surprisingly "OK" for many cases (at least that's what the blog article I linked claims). (Also check official document on this topic.)

Nevertheless, I would use PostgreSQL.

→ More replies (4)

3

u/Jaggedmallard26 Oct 14 '25

SQLite is great for production so long as you aren't using it as a client server database engine. There are plenty of usecases for sqlite.

→ More replies (2)

5

u/Bot1-The_Bot_Meanace Oct 14 '25

There's DBs on my work place that were already running when Yugoslavia still existed

4

u/Kitchen-Quality-3317 Oct 14 '25

I have a db in production that was created before we landed on the moon... The last write to it was probably 30 years ago, but it's still there.

3

u/No-Clue1153 Oct 14 '25

Idk i’ll try it and find out, 1 sec

6

u/wite_noiz Oct 14 '25

He never returned... We'll remember you, brave Redditor!

2

u/Blue_Moon_Lake Oct 14 '25

And then you're missing a AND x=y when you accidentally type enter.

→ More replies (16)

25

u/WillingLearner1 Oct 14 '25

Turn off auto commit nephew

44

u/mmhawk576 Oct 14 '25

You’ve not lived until you’ve accidentally truncated the wrong table

11

u/DrMerkwuerdigliebe_ Oct 14 '25

Or deleted the wrong database

7

u/OfficeSalamander Oct 14 '25

Everyone does it once. Mine was in 2012

→ More replies (1)

20

u/IWishIDidntHave2 Oct 14 '25

Look, all I'm saying is. Is...... Is that when I worked at a large University in the UK, there may have been an incident. Because I may have had unrestricted access to prod. And I may have been using SQL Query Analyzer to update a student's surname. And possibly, just for a few, brief, panicked moments, it may be that all students in the University shared the same surname.

12

u/dekeonus Oct 14 '25

one big (un)happy family

→ More replies (1)

17

u/Joker-Smurf Oct 14 '25

That is why you do

Select * from x where y;

Then after you are happy that you aren’t going to fuck everything right up, add “begin transaction;” in front of it, then replace “select *” with “delete”.

Then you run the delete statement and, assuming the number of deleted rows is correct, finish it off with “commit;”

15

u/FrozenHaystack Oct 14 '25

Reminds me of that one time I set up a query like the following:

DELETE FROM TableA WHERE Id IN (
    SELECT Id FROM ThingsToDelete
)

Just that I didn't know at that point in time that the database engine we use treats an empty sub select as TRUE, so it dropped the whole table.

9

u/arbitrary_student Oct 14 '25

The fact that it implicitly casts an empty select to a bool is already bad enough, but what unhinged psycho decided it should be TRUE?

12

u/_nathata Oct 14 '25

So is rm -rf /anything, because even tho you can't remove the root without an extra flag, in many occasions you will be writing something that starts with /usr or smth like that.

12

u/GL510EX Oct 14 '25

rm -rf ./

squints suspiciously

3

u/Tipart Oct 14 '25

fr, because rm -rf /. will nuke your system without actually asking.

3

u/Thisismyredusername Oct 14 '25

I'd just go deeper in wit cd and then do rm -r (directory I want to delete)

12

u/dijalektikator Oct 14 '25

I hate languages with more "human readable" syntax, it doesnt work for anything other than the simplest expressions. A complex SQL query is anything but readable and would benefit from a more "programmy" syntax.

10

u/CrushgrooveSC Oct 14 '25

That’s why the language requires semicolons. Stop having your tools insert them for you. 🤷🏽‍♂️

5

u/v1akvark Oct 14 '25

Semicolons used to be optional in SQL Server. Don't know if they changed that in later versions.

9

u/SirFoomy Oct 14 '25

First write your DELETE statement as SELECT statement. If the result is what you want to DELETE substitute SELECT * with DELETE and hit that enter key. This was the very first thing I was taught about Databases during my apprenticeship.

7

u/kuncol02 Oct 14 '25

No coffee wakes you up like message from technical assist with question "Is there recycle bin in SQL? I accidentally forget WHERE clause."

6

u/[deleted] Oct 14 '25

What SQL editor are you using that runs commands on enter? Ones I use have a run button and also transaction control so you have to press a commit button to actually apply any changes.

2

u/ak_miller Oct 14 '25

Yeah default in SQL Developer for instance is Ctrl+Enter to run a query and F11 to commit, strangely I've never hit those in succession accidentally.

3

u/yet_another_newbie Oct 14 '25

SQL Developer (assuming you mean the Oracle one) also has an Autocommit option but it's disabled by default.

→ More replies (5)

5

u/grundee Oct 14 '25

I wish syntax was DELETE ALL FROM ...

Where either ALL xor WHERE must be specified.

It makes it very clear what you want and catches the worst case scenarios. The default is the opposite of a failsafe: fail massively, catastrophically, and irreparably.

4

u/Deemonfire Oct 14 '25

    Select *

    -- delete

    From x

    Where y

I like to do this, so that i can be sure ive got the right data lined up for my delete.

I would use transactions but spark tables don't have them, unless you're using specific implementations

→ More replies (1)

4

u/Last-Egg6961 Oct 14 '25

There are a bunch of solutions to this but one ive not seen from scrolling which I prefer is a CTE

With DataToDelete as (

Select * from table where

)

Select * from DataToDelete

--Delete from DataToDelete

Just switch the comment to the Select after your confirm the dataset only contains the rows you want to delete.

4

u/MrDilbert Oct 14 '25

Frankly, WHERE in DELETE should be a required part of the query. If you want to delete everything in the table, you can explicitly use TRUNCATE.

 I mean, even with WHERE being required, you can still compose a query that will delete records you didn't want to, but at least it would make you think about the conditions...

2

u/BigBossYakavetta Oct 14 '25

There is big difference between DELETE FROM <table>; and TRUNCATE <table>;

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

4

u/MealieAI Oct 14 '25

Who has this kind of access on a Production system? Also, excuse my ignorance, maybe its because I've been stuck in an Oracle system for a while, but isnt there a "commit" that needs to happen first?

4

u/sgtGiggsy Oct 14 '25

Why would you use a client that executes the statement on hitting Enter though?

10

u/Zatetics Oct 14 '25

Do people not write this externally in n++ or vscode or something, or at the very least commented out? My gosh, some of you live dangerously. It's one button press (F5 in mssql) away from disaster.

8

u/adamMatthews Oct 14 '25

If it’s only one keypress away from disaster, you should reconsider how your database browser is set up.

If you’re using something like psql, get it in a transaction. If you’re using something like DBeaver or DataGrip, mark the connection as production so it makes you confirm every update.

→ More replies (2)

3

u/leaningtoweravenger Oct 14 '25

That's why you usually need a ; at the end and you have to type it only after you read it again

3

u/[deleted] Oct 14 '25

why is this even enabled on any database by default? it should get rejected and if you want to update everything then you should have to add where 1=1 explicitly

3

u/lightwhite Oct 14 '25

My wisdom I got from my mentor that I learned 20 years ago when I was a rookie in the industry. He told me that all these lessons were written in bliss, sweat and tears of someone before me used as ink.

  • If you are just deleting small amount records, export them first so that you can load them back in case it was wrong to delete them. In case of full tables worth of data, dump the whole table.
  • Make a full backup of the DB first! Even if you have automated once. And try to restore that backup. If restore is successful, delete the records on the restore first and then test. A backup that you never restored to test is not a backup. Use this moment to test your might.
  • Always select the things you wanna delete first to confirm you are deleting the right things. Then write your delete query in a text editor first and copy without the new line.

3

u/JackNotOLantern Oct 14 '25

That's why you always start from "SELECT...", run it, see what will be affected, then load the command from history and replace SELECT with DELETE

Also, use transaction

3

u/the_hair_of_aenarion Oct 14 '25

rm -rf ~/code/old Project

Half way through typing that I'm sweating nervously

3

u/[deleted] Oct 14 '25

I always wrote is as select * from first and then change ot to delete 

3

u/-Nyarlabrotep- Oct 14 '25

First you write the select, then once you verify the result you turn it into a delete/commit. If there are a lot of rows you use rowcount and multiple commits to limit the number of rows affected for each transaction.

3

u/ill-pick-one-later Oct 14 '25

SELECT *

-- DELETE

FROM table

WHERE condition

Guarantees no deletion until you are ready.

3

u/CalliNerissaFanBoy02 Oct 16 '25

You did start an Transaction right?

RIGHT?

7

u/lrosa Oct 14 '25

There are many ways to avoid it.

One is to SELECT first before UPDATE or DELETE

Another is to make a syntax error on purpose before completing the WHERE

Another one is write the WHERE first and the DELETE after (this is especially if you paste the WHERE condition from somewhere else where you tested it)

5

u/jam_pod_ Oct 14 '25

Or, and hear me out, ‘START TRANSACTION’

5

u/Fucking_Karen Oct 14 '25

Well? Are you just going to leave it open?

Either you finish that transaction or I'm going to have a serious word with your manager.

→ More replies (1)

2

u/MostTattyBojangles Oct 14 '25

I prefer to physically remove the enter key until I’m ready to execute 

2

u/wristcontrol Oct 14 '25

If Enter executes, how do you get newlines in your SQL?

2

u/ILikeLimericksALot Oct 14 '25

Pressing enter doesn't run the query in any IDE I've ever used. 

2

u/inwector Oct 14 '25

Except, enter doesn't do anything except to go to the next line. You need to hit execute or use ctrl e.

2

u/fahqurmudda Oct 14 '25

Hitting 'enter' does not submit SQL statements....

2

u/korneev123123 Oct 14 '25

No semicolon on accidental enter hit, no query would be executed.

Additional possible measures:

  • start dangerous session with BEGIN to start transaction
  • start query with comment, delete it before execution (works well for shell too)

2

u/Vegetable-Viking Oct 14 '25

That is why I learned to first type SELECT * FROM x WHERE y
And only after I confirm that this returns the data I want to delete, I remove the SELECT * part and replace it with DELETE.

2

u/[deleted] Oct 14 '25

Only noob do it this way. Select first !

2

u/Sync1211 Oct 14 '25

Unless you write a select statement and replace the SELECT with DELETE once you know that it works.

2

u/DoctorWaluigiTime Oct 14 '25

Which is why you don't type it sequentially. And why you use transactions. And why you have regular backups. Multiple layers of both "preventing a mess" and "reverting a mess."

2

u/chipmunkofdoom2 Oct 14 '25

That's why you start by writing a SELECT to see what you're going to delete first. If everything looks good, swap the SELECT * with a DELETE [Table Alias]

2

u/AlecsVeyo Oct 14 '25

"WHERE" should be always required, use "WHERE true" if you want to nuke everything

2

u/No_Preparation3429 Oct 14 '25

That's why, no auto-commit. Or ССЗБ

2

u/chisleu Oct 14 '25
-- delete from x where y
``

Then remove the comment. Some people prefer to type into a notepad/etc and copy/paste the SQL.

2

u/horizon_games Oct 14 '25

Get in the habit of writing SELECT first, checking that it's actually what you want to delete, then switching the syntax. Or use the multitude of apps that make you commit after an operation

2

u/Ok-Half-3766 Oct 14 '25

I’ve never typed delete first. I always write my queries as a select statement first then change the select * to delete. Well, after that one time…

2

u/Slggyqo Oct 14 '25

Write it as a select statement first, with a limit.

You can sense check the result, and then just convert it to a delete Statement.

→ More replies (1)

2

u/FreakDC Oct 14 '25
  1. Set your IDE to read only mode so you have to confirm any write query (at least for production DBs)
  2. Start with writing SELECT * FROM x WHERE y; before you replace the SELECT * part with DELETE first, it's good practice anyway.

2

u/Much-Tomorrow-896 Oct 14 '25

SELECT FROM x WHERE y

then

DELETE FROM x WHERE y

2

u/ineedhelpbad9 Oct 14 '25

How is the 'delete from x' default to delete everything? Why doesn't it default to nothing and force you to specify everything with a 'where *'

2

u/Capt_Kiwi Oct 14 '25

Always always always BEGIN TRAN first

2

u/Warranty_V0id Oct 14 '25

DBeaver warns you when you query an update or delete without a where condition. Also i always start with select * from x where y to double check what i'm deleting. That's quicker than finding the backup 😅

2

u/bkstr Oct 14 '25

write it as a select first then change to a delete

2

u/ShakesTheClown23 Oct 15 '25

rm *.tmp has entered the chat

2

u/Abhinav1217 Oct 15 '25

Always use BEGIN; to start transaction, and then COMMIT; when working.

4

u/SuspiciousBread14 Oct 14 '25

What fucking db tool uses "Enter" to perform an SQL?

3

u/theevilapplepie Oct 14 '25

All the database clients I've used ( the db console apps provided by the db ) do this, it's assumed your SQL commands are newline delimited, with the exception of Microsoft's sqlcmd anyway.
Examples of this being the case are Oracle, MySQL, & Postgres.

1

u/visheyra Oct 14 '25

either select what you want to delete or put a little comment at the start of the line and remove it once happy with the query

1

u/gwmccull Oct 14 '25

Let the intrusive thoughts win 😈

1

u/HosTlitd Oct 14 '25

Not at all. Who starts query with mandatory semicolon?

1

u/Lost-Droids Oct 14 '25

I always right deletes from right to left.

1

u/Warzone_and_Weed Oct 14 '25

That's why you always start with --

1

u/radek432 Oct 14 '25

That's why I always follow this order:

  1. Start typing with WHERE 0=1 first
  2. Add the DELETE FROM part
  3. to the WHERE statement add AND with proper condition
  4. change the 0=1 to 0=0
  5. run the query

1

u/silvers11 Oct 14 '25

I just write the whole thing in a text editor and then copy paste once I’ve reviewed it

1

u/mr2dax Oct 14 '25

Pretty sure many sophisticated engines don't allow delete from without a where clause.

1

u/doker0 Oct 14 '25

Now we have another goto candidate?

1

u/Loose_Conversation12 Oct 14 '25

Begin tran

Rollback

Then put your query in between

1

u/ScaredyCatUK Oct 14 '25

limit 1 CTRL-A delete from x where y CTRL-E ;

1

u/[deleted] Oct 14 '25

Why don't they put a confirmation before doing such ops? It could literally save from such accidents

1

u/Webers_flaw Oct 14 '25

After making an oopsie in prod and loosing a tiny bit of hair, I always start transactions before deleting/updating and only commit if the num of rows affected is the same as the selected ones

1

u/Altourus Oct 14 '25

Begin transaction

// code goes here

Rollback transaction

Always the first things I type before doing an update or delete query, then run once to make sure I get the results I want before selecting just the query to run. You will only have to accidentally update an entire table to point to a single image once in your career as a developer, then some senior fixes it, and tells you to do that. You will never stop doing that or telling everyone around you to do that.

1

u/alphacobra99 Oct 14 '25

delete * from users where True

1

u/CorrectBuffalo749 Oct 14 '25

No pain no gain

1

u/erdelll Oct 14 '25

I write intentionally wrong at first like "deletek ..." or "inseert..." at first then check it

1

u/neoteraflare Oct 14 '25

This is why I hande the commits manually instead of autocommit. Also DBeaver asks me if I'm sure about running a delete without where.

1

u/UpvoteForGlory Oct 14 '25

I once had a code that said update table set netamount=variable, vatamount=grossamount-variable where id = xxx With variable being a calculated amount. This worked great until the variable at some point was a negative amount and I learned how comments work in SQL.

1

u/[deleted] Oct 14 '25

Noone is running SQL directly on production databases. There would be no audit trail and you'd not be ISO 27001 compliant.

1

u/xgabipandax Oct 14 '25

So don't hit enter until you finished writing it?

1

u/nollayksi Oct 14 '25
  1. Always start your query with beginning a transaction unless you are in a local dev environment you can rollback in other ways easily
  2. Always do your query by writing it inside /* */ block comment and highlight the query when its done. Any sql client supports running only the linea you have highlighted

1

u/_crisz Oct 14 '25

This is the way I work:

- execute query

- check Datagrip output. Does it say it deleted the number of rows it was expected to delete?

- if yes, commit

- if no, rollback

If you have transaction mode set to auto, then you have a bigger problem

1

u/LaMortPeutDancer Oct 14 '25

What is a semi-column ?

1

u/Character-Travel3952 Oct 14 '25

If you are a cowboy who hates transactions then you can atleast write it as a selectment statement and check the count. Before pulling the trigger

1

u/[deleted] Oct 14 '25

You make a select and when you are satisfied with select you rewrite it to delete.

If you just write delete statement, you are a mad man.

1

u/schepter Oct 14 '25

That’s why I modified the sql template file within BEGIN TRANSACTION and END TRANSACTION. So all new query windows are a little bit safer. 

1

u/Wojtek1250XD Oct 14 '25

First write SELECT until you create your desired WHERE statement, only then replace the SELECT with DELETE

1

u/TerrificFrogg Oct 14 '25

• RFC 1970 New syntax:

FROM table x WHERE y DELETE | SELECT

1

u/SynapseNotFound Oct 14 '25

Depends how you run your query

Enter never does anything but new line the ways i make a query. Inside an IDE or building a script inside database management software