Most databases operate in autocommit, however there is a semicolon after delete users - it's presumably removing all users and foreign key associations and it's taking its time to complete, before reporting where is = 12345 is an invalid SQL statement.
Most production environments run with the --i-am-a-dummy flag (or its boring alias --safe-updates) which prevent exactly this scenario and will force you to do DELETE FROM users WHERE 1 if you really want to delete everything.
This is for MySQL but I'm sure there's something similar in other databases.
Is also fairly common to have a safe update mode enabled, in which case it blocks deleted and updates without a where clause restricting it in some way. I think it might even require including a key column, but that part I’m not sure about.
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).
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.
So a colleague did this, except his db manager had tx commit mode manual and he forgot to commit the tx after grabing a whole table lock. Took us a bit to figure out why some of our services were hanging during startup (waiting for the locks to free up)
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.
What are you boomers talking about, I just ask chat to write me a million dollar product and it stores passwords in plain text in an excel spreadsheet /s
I used to work with this small web dev company, one day a guy admitted that they didn't pay for hosting for small projects, the server was a minitower PC in their office.
When the cleaning lady pulled the plug by mistake, several sites went down and nobody noticed until next morning
In an ideal world, there would be a local environment (that's your pc, nothing deployed), a Dev environment (you can deploy things still being developed, it might have bugs, it might be wonky, this is to test things in a deployed state, which could actually differently to when running locally), a staging environment (anything deployed to staging should be thoroughly tested by Devs, and is now ready for other people to test it (UAT tests). Staging is like a test prod environment), and a live/production environment (this is what the users use, anything deployed here is fully tested by multiple people)
We use four standard environments, DEV for development and any exploration. TEST for testing by the team, could still be different because of unreleased developments, QA for user tests, which is regularly cloned from PROD and doesnt contain any developments that won't be released, and ofc PROD, where only admins have full access for any emergency
You jest, but this is the case at my company. Except it's the CTO.
Our environment? Open source on prem tools. It's literally just spinning up a new VM. Our data needs? Less than 1TB.
I've broken prod quite a few times. Every time it happens I mention it could be avoided with a proper test environment that's iso-prod. Still waiting after 2 and a half years.
I was traumatized by a query I did almost 20 years ago, ever since I put "LIMIT 2" on queries where i'm just manually deleting one thing... if I see 2 rows modified I know I messed up. I even do it with transactions because of the trauma.
At $PREVIOUS_JOB, we had a nightly job that deleted old users. It saved everything to temporary files, and halted if the number of users had shrunk by 10% or more.
Where I work their manager just puts in a ticket with my team and gets mad about why the data is missing. Then we trace the transaction back to their dev's user.
One time a dev team begged us to be able to use the DBMS benchmarking tool, so I spent an hour going over its use and sent them the docs on it. Two hours later, the dev database they were working in was dropped and I had a ticket from them demanding I restore it with a full explanation of why it happened. It was the dev environment, and we don't keep backups of it (because why would we? I've seen the shit they do there).
I mean out of all the data you could accidentally replace, passwords would be the easiest to restore from a backup reliably since they don't change frequently. The special few who get locked out because you restored an old one just have to do a reset.
It would be a bit of a race to replace them before it was a problem, but at least it isn't as much of a headache as wiping out data entry that needs to be redone by other users.
I'd like to think that production mistakes happen and should be forgiven, provided that no actual malicious intent was at play.
And hopefully this starts the conversation of "Wait, should people actually be able to do this directly against the production database?" and making sure this oversight can't happen again.
Well, it can be very useful, but you should know what you are doing and manual query execution should be disabled for most users in production anyway (this is the real problem here if you ask me).
In my last company we had to regularly change stored procedures and their dependencies on user defined table types (which means you have to drop the procedure, drop and create the type and recreate the procedure). Without the highlighting feature that would have taken even more time as you would have to copy stuff around instead of simply generating the drop and create scripts and executing parts of them in the correct sequence.
I worked at a bank(!) where someone did something similar that disabled the password verification check so anyone could log in without using a password.
Somehow no customers, we think at least, found this before it was reverted. Everything was developed in production there when I joined. Most chaotic but also fun work environment I've been in.
That's why I prefer datagrip, it's almost idiot proof. So you'd typically get a warning if you attempt to run a delete statement without a where clause. You then need to deliberately dismiss this prompt to continue.
Hehe. When I was young, with soul full of hope and head full of hair, I did some testing on fresjly developed inventory managemet system DB. Our dummy records were in the id range of 0 to 99, and I decided to clean them up to. With sql script "delete * from Components where id<100". Guess the error.
Before SQL Tools gave you a warning about running mutable queries without WHERE clause I had practice to wrap all such dangerous queries in transaction and checking affected count before commiting. It did save me a few times.
In most SQL setups I have used, the first line would execute fine and then the second would error out.
Haven't done anything quite this bad, but I definitely have been in situations where I wonder "why has this simple query been running for 10 minutes - oh, the WHERE clause is half missing / there's a cartesian join in there / that's not my dev schema" (delete whichever is inappropriate)
Yeah the first line would run no problem. But if you were sensible enough to do this in a transaction, the error on the second line would cause a rollback.
Already like the 4th time I am seeing this. Can your SQL Client really do this? My SQL Client does block delete statements without where block. I would have to write DELETE FROM USERS WHERE true; to delete all rows
I ran a truncate table statement on Oracle DB once . I thought I was running it on test DB, but it was production.
Delete or drop would be much better in that scenario (table dropped goes to recycle bin; with delete, the archive trigger we have on every table would still work).
The good thing - there were no changes on this table that day, so I could get it back via daily database dump.
I had a young dev that tried to make a copy of the prod database, on the prod server (real hardware with shared dBase files), during business hours with 200+ users working. Too bad he forgot the order of the DOS copy command. He copied his old copies onto prod. No one knew what he was doing. Never asked for help. He was gone the next day
Something similar happened to me. Deleted something from the intranet of one of the largest TV stations in Germany (ProSieben Sat.1 Group). Thanked god on my knees for rollback (it was an Oracle DB)
No problem! Since the query most likely running in a sandbox, and if it would somehow make it's way to production then there is PIT backup available to recover from. Right?...
remember: if you're running a query against production, any query, send for an entire council of tech priests to beg the database's machine spirit first.
I feel like these memes are on the same level as the "missing ;" compiler memes. Does anyone actually do this in production? Running SQL commands is production if just asking to shoot yourself in the foot and if you do just use transactions.
I still don't understand why anyone would just manualy do stuff in the prod database. While a user should only be disabled, why doesn't your application have a user delete button? If you need to query the db just get a backup and do your analytics locally.
If you're working in a place where you manually delete things from prod databases for ANY reason - you aren't in a place where deleting all the users takes 30 minutes.
Guys we know that most tools have safety features against exactly this. But in this hypothetical scenario, the query is running for 30 minutes, meaning those clearly didn't exist.
What would help here is transaction logs and/or backups.
That why you run the SQL though a syntax checker first then start a transaction then check how many records would be effected before you commit the transaction and if your system is good those records are in the database’s equivalent of the trash bin (not visible to selects but still recoverable) even after you commit the transaction and/or there’s a copy of the database from a week ago you can recover the records from. Oh and if your devOpts is paranoid you won’t be able to run raw SQL instead you will need to build it with a special class that does all that for you and won’t allow submitting database changing commands without a where clause or manually switching off that safety.
Do people really not use built in features like transactions for destructive queue? Or if you want to see which specific rows will be affected before executing the delete you could run a with query/CTE and then use that temp table as the basis for your delete.
We once had an apprentice who updated all flats in all buildings of a government database to the same square footage because he forgot the Where. For about 750’000 flats.
Horrible design flaw in SQL there. The WHERE clause should be mandatory and to delete every row from a table it's `DELETE FROM users WHERE TRUE`, or, if your DB supports it, `TRUNCATE users`. Much harder to do either of those things by accident.
Same goes for UPDATE. I wouldn't be sorry to see it on SELECT either tbh.
Been there, done that. Nearly had a heart attack, but somehow, in the midst of the shock and disbelief, I managed to remember that we had (thankfully) enabled Flashback for the affected table.
Ahhh, shit. I was wondering what was the joke: that the ID is a different type and just plopping an integer there would be parsed as "any ID" or what... ?
Then I noticed the semicolon. Yeah, looks like I'd make the same mistake :D
108
u/Recent-Analysis-6880 Sep 22 '26
MySQL workbench has auto commit, meaning if you write a SQL query without "Start transaction" the data is gone.
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.
2.3k
u/PatientlyAnxiously Sep 22 '26 •
Why have a dev environment when you can YOLO it in prod
340
u/Altourus Sep 22 '26
I audibly gasped when I saw the semi-colon
718
u/coldnitrogen Sep 22 '26
Bro’s already started looking for a new job
584
u/WilmaTonguefit Sep 22 '26 edited Sep 22 '26
I remember a dude who missed a where clause once.
Except he only highlighted the first two lines and pressed F5...
215
u/musicplay313 Sep 22 '26
Team lead, staff engineer of our data engineering team couldn’t find the problem with this sql command. Yay!
62
u/q0099 Sep 22 '26 •
Pff! It will stumble on the first user that have associated records. Wait... Don't tell me... Don't tell me the cascade delete is on!
353
u/TorbenKoehn Sep 22 '26
Reality: SQL tool gives error message "You are running a mutable query without a where clause, you have to explicitly activate this in the settings"
46
u/Spinnenente Sep 22 '26
Second line is is not valid sql and would show this issue instantly. Unless the madlad ran this directly on prod with the sql cli.
41
u/why_1337 Sep 22 '26
Just use datagrip, it will tell you to fuck off. You would need to write
DELETE FROM Users WHERE 1 = 1;to delete everything from that table.30
u/Rainmaker526 Sep 22 '26
ROLLBACK TRANSACTION
20
u/CenderzeSwarm Sep 22 '26 edited Sep 22 '26
Begin transaction;
Select count(*) from Users where Id = 12345;
Delete from Users where Id = 12345;
Select @@ROWCOUNT;
Select count(*) from users where id = 12345;
Then either COMMIT TRANSACTION; or ROLLBACK TRANSACTION; depending on what your test shows
18
u/Living-Confection- Sep 22 '26
That's why I prefer datagrip, it's almost idiot proof. So you'd typically get a warning if you attempt to run a delete statement without a where clause. You then need to deliberately dismiss this prompt to continue.
9
u/FortuneDW Sep 22 '26
Happened to me once in prod, now i always bracket my update with begin tran and rollback.
It takes some time but no more time than having to fix the blast caused by a fucking typo.
9
u/UncleBaguette Sep 22 '26
Hehe. When I was young, with soul full of hope and head full of hair, I did some testing on fresjly developed inventory managemet system DB. Our dummy records were in the id range of 0 to 99, and I decided to clean them up to. With sql script "delete * from Components where id<100". Guess the error.
6
u/Hooch180 Sep 22 '26
Before SQL Tools gave you a warning about running mutable queries without WHERE clause I had practice to wrap all such dangerous queries in transaction and checking affected count before commiting. It did save me a few times.
6
u/Jeevesh_Sharma Sep 22 '26
Don't worry, it will run faster next time.
5
u/Academic_Broccoli670 Sep 22 '26
No problem, that's what the regular backup-restore tests are for. We did the restore tests, right?
... Right ...?
25
u/RedditLuvsCensorship Sep 22 '26
Hate to be the Debby downer here but running that wouldn’t execute due to syntax error.
3
u/PrizeSyntax Sep 22 '26
When running delete statements directly, always run it with a select first, then just substitute the select works with delete
3
u/JewelerAggressive Sep 22 '26
Already like the 4th time I am seeing this. Can your SQL Client really do this? My SQL Client does block delete statements without where block. I would have to write DELETE FROM USERS WHERE true; to delete all rows
3
u/bakus33 Sep 22 '26
I ran a truncate table statement on Oracle DB once . I thought I was running it on test DB, but it was production.
Delete or drop would be much better in that scenario (table dropped goes to recycle bin; with delete, the archive trigger we have on every table would still work).
The good thing - there were no changes on this table that day, so I could get it back via daily database dump.
3
u/FallenLeaf54 Sep 22 '26
Isn't "delete from" transactional? If it's still running, cancelling it should auto rollback right?
3
u/pdromeinthedome Sep 22 '26
I had a young dev that tried to make a copy of the prod database, on the prod server (real hardware with shared dBase files), during business hours with 200+ users working. Too bad he forgot the order of the DOS copy command. He copied his old copies onto prod. No one knew what he was doing. Never asked for help. He was gone the next day
3
u/Beaufort_The_Cat Sep 22 '26
“This’ll be a quick query, just a couple records..”
-30 minutes later…
3
u/BuyMyBeardOW Sep 22 '26
The real crime is having no transaction
3
u/arensb Sep 22 '26
"You're right! The semicolon does terminate a statement. Would you like help updating your resume?"
5
u/PloxNox65 Sep 22 '26
Really? 30 minutes for that ?
2
u/windows300 Sep 22 '26
My favorite feature of datagrip is the warnings it gives you when deleting or updating without a where clause.
I also rarely ever connect to anything other than the dev, and if I do I have a second person watching and doing shit as a pair.
2
u/rdrunner_74 Sep 22 '26
I brought down the SQL Server once using a deployment script... But had to fix it myself in under 30 minutes.
I was glad i took over as DBA and updated the backup methods shortly before that...
2
u/KingValidus Sep 22 '26
Confirmed, I am fun at parties.
2
u/4N610RD Sep 22 '26
I am about to dive into SQL, anybody willing to explain this?
2
u/mike_a_oc Sep 22 '26
Oooh. And autocommit is enabled! Just incase your day needed to be longer
2
u/IMKGI Sep 22 '26 edited Sep 22 '26
He didn't commit yet so it's fine.
2
u/Pengo2001 Sep 22 '26
Something similar happened to me. Deleted something from the intranet of one of the largest TV stations in Germany (ProSieben Sat.1 Group). Thanked god on my knees for rollback (it was an Oracle DB)
2
u/crashandburn Sep 22 '26
That would hopefully fail because of FK constraints in other tables...I can only hope.
2
u/KaneTW Sep 22 '26
ROLLBACK;
2
u/Frosty-Photograph103 Sep 22 '26
The query runs longer than 30min by design so you can browse job boards meanwhile.
2
u/v3ritas1989 Sep 22 '26
Are you not blocking delete commands without conditions?
2
u/SnooCapers4506 Sep 22 '26
No problem! Since the query most likely running in a sandbox, and if it would somehow make it's way to production then there is PIT backup available to recover from. Right?...
2
u/an_agreeing_dothraki Sep 22 '26
remember: if you're running a query against production, any query, send for an entire council of tech priests to beg the database's machine spirit first.
2
u/theepi_pillodu Sep 22 '26
Ha ha, I saw the extra semi-colon way too late. 😂
2
u/Aerosherm Sep 22 '26
I feel like these memes are on the same level as the "missing ;" compiler memes. Does anyone actually do this in production? Running SQL commands is production if just asking to shoot yourself in the foot and if you do just use transactions.
2
u/JayneWasRight Sep 22 '26
Could be worse.
DROP TABLE users; COMMIT;
2
u/basshead17 Sep 22 '26
In what SQL engine would
WHERE id = 12345;be missing squiggly red lines?2
u/quietsamurai98 Sep 22 '26
Apparently this dev's onosecond lasts half an hour. Incredible.
2
u/slater_just_slater Sep 22 '26
Nothing more fun than that "quick clean up in prod".. be sure to do this at 4:45.pm on a friday.
2
u/Mutopiano Sep 22 '26
All I see is one giant bottle of Tres Commas on the delete key
2
u/-__-Malik-__- Sep 22 '26
LIMIT 1 dammit
2
u/sparkplay Sep 23 '26
This is not funny. I nearly had a heart attack and the last 10 years of my life flashed before my eyes. Go sit in a corner OP.
2
u/wgallios Sep 23 '26
How about, don't run update and delete commands directly on the prod database. Create a script, test on local dev, then push to prod.
3
u/oweiler Sep 22 '26
Most (modern) SQL tools will prevent this.
1
u/soundwave_sc Sep 22 '26
My heart sank reading that script.
1
u/Next-Wrap-7449 Sep 22 '26
We had a guy that during a test on the dev environment somehow sent 40000 emails with old orders to be prepared for shipping at the warehouse.
1
u/WriedGuy Sep 22 '26
A small ; can change someone's life
1
u/amorpheuse Sep 22 '26
I still don't understand why anyone would just manualy do stuff in the prod database. While a user should only be disabled, why doesn't your application have a user delete button? If you need to query the db just get a backup and do your analytics locally.
1
u/lllyyyynnn Sep 22 '26
how did it pass the migration test
1
u/Wojtek1250XD Sep 22 '26
Well, the solution has always been to formulate your querry with SELECT first.
1
u/assistmerchant_ Sep 22 '26
Because it's deleting your career, one row at a time.
1
u/Hairy_Meaning_73 Sep 22 '26
That's why you always try to do a SELECT with the same query instead.
1
u/418_TheTeapot Sep 22 '26
Punctuation matters
1
u/maxip89 Sep 22 '26
SET autocommit = on;
1
u/mookanana Sep 22 '26
rollback;
phew.
1
u/TypicallyThomas Sep 22 '26
"I figured it would be quicker to do this in prod"
1
u/mrwishart Sep 22 '26
1
u/Kamay1770 Sep 22 '26
Begin transaction...
Rollback transaction...
Check rows affected.
1
u/asensitives Sep 22 '26
oh, god... oh, man... oh, god... oh, man
1
u/JackstonVoorhees Sep 22 '26
Every modern SQL engine protects you from executing DELETE FROM without a clause.
1
u/DuckShapedGoose Sep 22 '26
Guys we know that most tools have safety features against exactly this.
But in this hypothetical scenario, the query is running for 30 minutes, meaning those clearly didn't exist.
What would help here is transaction logs and/or backups.
1
u/Miiohau Sep 22 '26
That why you run the SQL though a syntax checker first then start a transaction then check how many records would be effected before you commit the transaction and if your system is good those records are in the database’s equivalent of the trash bin (not visible to selects but still recoverable) even after you commit the transaction and/or there’s a copy of the database from a week ago you can recover the records from. Oh and if your devOpts is paranoid you won’t be able to run raw SQL instead you will need to build it with a special class that does all that for you and won’t allow submitting database changing commands without a where clause or manually switching off that safety.
1
u/mohammad5253 Sep 22 '26
We need to log a performance jira for it /s
1
u/nawanamaskarasana Sep 22 '26
I've done something similar on live server. Admin did hourly database dump so no problem restoring.
1
u/rocket_randall Sep 22 '26
Do people really not use built in features like transactions for destructive queue? Or if you want to see which specific rows will be affected before executing the delete you could run a with query/CTE and then use that temp table as the basis for your delete.
1
u/dzendian Sep 22 '26
If you're going to do this, at least normalize wrapping it in a transaction. Amateur move.
1
u/telas100 Sep 22 '26
Deleting one table in 30min? Have you tried Truncate?
1
u/Strange-Spot-3306 Sep 22 '26
We once had an apprentice who updated all flats in all buildings of a government database to the same square footage because he forgot the Where. For about 750’000 flats.
1
u/darcksx Sep 22 '26
Always have a deleted_at column, you then select deleted at columns then deleted them.
1
u/waiver-wire-addict Sep 22 '26
JavaScript: I found the missing semi colon, it got added to this SQL query…
1
u/jolharg Sep 22 '26
Oh no
1
u/traphousethrowaway Sep 22 '26
Did event comment out the delete clause, have mercy and say your prayersc
1
u/stools_in_your_blood Sep 22 '26
Horrible design flaw in SQL there. The WHERE clause should be mandatory and to delete every row from a table it's `DELETE FROM users WHERE TRUE`, or, if your DB supports it, `TRUNCATE users`. Much harder to do either of those things by accident.
Same goes for UPDATE. I wouldn't be sorry to see it on SELECT either tbh.
1
u/MintyBullet Sep 22 '26
I did something similar with an update statement my first year, thank god it was in dev though
1
u/Cybasura Sep 22 '26
The Onosecond
1
u/F3LyX Sep 22 '26
True story.
I once accidentally deleted the entirety of active directory at the census.
1
u/the_millenial_falcon Sep 22 '26
It’s fine, just press Ctrl-Z.
1
u/JohnArcher965 Sep 22 '26
I dont do much SQL - the ; at the end of the first line is the problem right?
1
u/Brittany_Delirium Sep 22 '26
This is why we run all potentially damaging queries in transactions
1
u/Catbraveheart Sep 22 '26
Been there, done that. Nearly had a heart attack, but somehow, in the midst of the shock and disbelief, I managed to remember that we had (thankfully) enabled Flashback for the affected table.
1
u/Wubbajack Sep 22 '26
Ahhh, shit. I was wondering what was the joke: that the ID is a different type and just plopping an integer there would be parsed as "any ID" or what... ?
Then I noticed the semicolon. Yeah, looks like I'd make the same mistake :D
1
u/Blecki Sep 22 '26
This would just get rejected by the query compiler and nothing would run...
1
u/YouJellyFish Sep 22 '26
Surely it immediately throws an error with a foreign key and this does nothing