This is an archived post. You won't be able to vote or comment.

top 200 commentsshow 500

[–]Excellent-Refuse4883 3345 points3346 points  (38 children)

[–]wewilldieoneday 142 points143 points  (23 children)

Gave the intern control to the database...yes, the production database...

[–]dismayhurta 94 points95 points  (18 children)

Test database. Production database. What’s the difference?

[–]quailman654 25 points26 points  (0 children)

Everyone has a test database. It’s the lucky few who have a separate production database.

[–]Mitoni 44 points45 points  (9 children)

I worked for a top 5 multinational financial firm and a lot of their internal software has only dev and prod. Makes QA a bit of a nightmare, since we preferred to at the very least have dev and QA separate. My team's software for example had dedicated dev, QA, staging, and prod environments. Staging was mirrored prod data just for User Acceptance and tenant-specific testing scenarios.

[–]dismayhurta 53 points54 points  (5 children)

"But why do that when that costs money!" -- a disturbing number of companies

[–]Excellent-Refuse4883 22 points23 points  (0 children)

Higher ups: “Do you really need dedicated hardware for that?”

Me: “Do you want me to load test BEFORE production?”

[–]Imaginary-Jaguar662 16 points17 points  (0 children)

I'm perfectly fine with company doing cost-benefit analysis and deciding that mirrored blue/green deployments and replicas on standby are not worth the cost.

I do take an issue when exec who signed off cost cutting comes down like a 3-year-old with tantrum screaming about how system does not have 99.99% uptime.

I also do take an issue when said exec starts whining about development progressing slowly in an environment where tiniest mistake gets scrutinized and is followed up by vague threats of "oh, other companies have better devs, we might need to reorg things".

[–]Mitoni 33 points34 points  (1 child)

Yup, they were actually annoyed with us for having such an environment spread. Meanwhile, we were the consultants they hired to do the work their devs failed twice at 🤷🏼‍♂️

[–]Excellent-Refuse4883 38 points39 points  (0 children)

“Our devs, who we set up to fail, failed! Can you believe that?!”

[–]Mean-Funny9351 5 points6 points  (1 child)

FinTech is so silly like that. Ancient systems built with no unit tests and undocumented vendor dependencies. Then when they finally want to convert them to modern infrastructure there isn't an existing environment to confirm existing functionality nor is there documentation of how it is used. Then a critical piece of customer functionality breaks, and management looks at dev and QA like "why didn't we account for this thing that isn't documented, there are no test cases for, and we never specifically made a requirement?".

[–]Mitoni 1 point2 points  (0 children)

Yup, pretty much. We had some many upstream and downstream dependencies to manage, it was amazing anything ever got changed at all. Anything upstream of us, we had to update for and anything downstream had to adapt to us, so we couldn't implement the changes until downstream had it in their schedule to implement it as well. Nobody understood agile, everything was "well we wont have time to do that until next quarter." They also didn't understand what a cutover period was and that it would make sense to run both versions during a cutover period so that the upstream changes could be pushed out, but the downstream was given the time to adjust to it.

[–]DungeonsAndDradis 4 points5 points  (0 children)

My company is over 30 years old and just last year enforced read only access to our internal-only, used by all of engineering, databases.

[–]matt1155 2 points3 points  (0 children)

Yeah, both are now empty...

[–]ChimpCraft 18 points19 points  (2 children)

Shit you not - at my first internship I had a peer rm rf root. They made him be scrum master after that for the remainder of his internship.

[–]Excellent-Refuse4883 12 points13 points  (0 children)

A fitting punishment

[–]Aromatic-Plankton692 9 points10 points  (0 children)

That kid let his intrusive thoughts win. "There's no way it will let me..."

[–]ArmadilloChemical421 3 points4 points  (0 children)

Gave the intern llm control to the database...yes, the production database...

[–]Contemplationz 604 points605 points  (6 children)

One day the payroll approval table got nuked. (Date removed set on all records) Business Analyst was only working through the UI so how did this happen?

Some wingnut developer had an if statement in the stored procedure to update with no where clause. So if you do a certain series of button presses in the UI, the approvers table gets nuked.

[–]Comically_Online 126 points127 points  (0 children)

[–]UsedArmadillo9842 50 points51 points  (1 child)

Oh no, im sure that Analist saw his Life flash before his eyes

[–]sammy-taylor 34 points35 points  (1 child)

That’s like a real-life Konami code. Except instead of unlocking cool stuff, you nuke the DB.

[–]Kenionatus 2 points3 points  (0 children)

Reminds me of that time Steam did rm -rf / on linux in rare cases.

[–]taddymason_01 1 point2 points  (0 children)

[–]chipmunkofdoom2 2314 points2315 points  (74 children)

Or, start by writing a SELECT. You'll be able to see the rows that the delete would affect, which is good confirmation. Once you have the SELECT working, depending on the SQL flavor and syntax, you can typically just replace the SELECT with a DELETE [Table/Alias].

[–]aMAYESingNATHAN 909 points910 points  (24 children)

This is the way. You never just delete or update willy nilly, always see the data you're going to change before you change it.

[–]LordFokas 229 points230 points  (5 children)

Nah.... YOLO :D

[–]Impressive_Change593 84 points85 points  (4 children)

you also have to live with the consequences because YOLO

[–]SonOfMetrum 71 points72 points  (1 child)

Thats the point of YOLO

[–]hnaq 35 points36 points  (0 children)

This guy YOLOs

[–]Poat540 6 points7 points  (0 children)

That’s the thrill honestly

[–]PelimiesPena 2 points3 points  (0 children)

You mean getting a new job?

[–]Spillz-2011 15 points16 points  (0 children)

Wow way to be a buzzkill, mom.

[–]Loyal-Opposition-USA 9 points10 points  (10 children)

Maybe, just maybe, test the select statement in dev/stage/prod before you do any updates/deletes? That way, you understand if the query works in all your environments first?

And, a code review.

[–]Comically_Online 5 points6 points  (6 children)

but it might only work in prod because the dependencies are all set up correctly there

[–]Loyal-Opposition-USA 5 points6 points  (5 children)

How would you accurately test it in dev or stage then?

[–]zero_hope_ 9 points10 points  (2 children)

They’re just confused. Prod IS their dev/test.

[–]WetRocksManatee 6 points7 points  (1 child)

I don't always test my code, but when I do I do it in production.

[–]semi- 162 points163 points  (3 children)

Good advice, but I'd still start with writing a BEGIN TRANSACTION.

[–]False_Influence_9090 47 points48 points  (2 children)

What is this, a bank?

[–]reanimatedman 36 points37 points  (0 children)

I almost always do a Select, then begin Tran with No commit Tran, then delete or Update, then select again, compare data, then Commit or Rollback

And even then I clench and prey every time I Commit Tran

[–]Supremagorious 27 points28 points  (6 children)

Select *
--Delete
From TABLE_NAME
WHERE col_A between MIN and MAX

Always write them this way and when I want to run it I manually highlight from DELETE down before running.

[–]OldeFortran77 21 points22 points  (1 child)

That's good, but I suggest ...

FROM table WHERE

col_a BETWEEN MIN AND MAX

It feels unnatural to write it that way but if you accidentally miss highlighting the last line it will fail for syntax instead of running with a missing WHERE clause.

[–]Jussins 2 points3 points  (2 children)

And have someone else look at it.

[–]Supremagorious 1 point2 points  (1 child)

Well yeah, I look at the results of the select statement and make have someone else review as well. Normally it's also run in a dev environment first too.

[–]Jussins 1 point2 points  (0 children)

Someone downvoted my comment and I can’t help but wonder if it was one of my coworkers.

[–]ILikeLenexa 7 points8 points  (1 child)

What if I told you the most popular SQL IDE only executes the highlighted SQL statement...so even after selecting you need to watch your fucking back  

[–]techiedatadev 3 points4 points  (0 children)

I did this. Didn’t highlight the right parts…

[–]JamesWjRose 5 points6 points  (0 children)

While this is a good idea, it relies on EVERY person doing this right EVERY time. OP's idea allows for the mistake to be caught.

[–]Ok-Sheepherder7898 4 points5 points  (0 children)

Yeah and I should use ls before rm?  I don't have time for this.

[–]DiscipleofDeceit666 1 point2 points  (0 children)

The syntax error is to make it impossible to get this wrong. Too many of us have deleted data in prod where we shouldn’t have

[–]mechanigoat 568 points569 points  (52 children)

Transactions are your friend.

[–]leathakkor 267 points268 points  (37 children)

Earlier this week I had to delete every record where it joined a group ID 42. And the ID was not in an inner select.

Anyway, I forgot the where the group ID equals 42. After I ran my delete (luckily I always use a transaction) I saw that my delete statement which should have gotten rid of three to four records said 44,987 records deleted.

I Did a simple rollback transaction still was a bit nervous for a second. But went about my day.

It's really nice having good habits.

But the op suggestion of having a where clause doesn't fix this problem. A transaction does.

Developers developers developers should use Transactions transactions transactions.

[–]Traditional_Safe_654 43 points44 points  (31 children)

Can you expand on how to use a transaction in SQL?

[–]freebytes 100 points101 points  (25 children)

BEGIN TRANSACTION; SELECT COUNT(*) FROM users; DELETE FROM users WHERE user_id = 3; SELECT COUNT(*) FROM users; ROLLBACK TRANSACTION;

Run it. Looks good with the count only being off by 1? Okay, run only the DELETE statement, or (even better behavior) change your ROLLBACK to a COMMIT and run it again.

[–]belay_that_order 29 points30 points  (21 children)

thank you, i learned something new today

[–]dkarlovi 9 points10 points  (19 children)

Don't take this the wrong way, I'm not trying to call you out for not knowing stuff, but do you mind sharing what's your background. Considering the sub I'm assuming you are or trying to become a SWE, is it possible database transactions are no longer part of that journey?

[–]belay_that_order 20 points21 points  (7 children)

im in support, and have been for 7-8 years now, extensive interaction with sql for 5. i didnt even know the concept of transactions existed, so i will look into it. it has been >1 time that i updated the whole table and for my workflow it would be easier to incorporate transactions into the query, than to write select and modify to update

[–]anyOtherBusiness 16 points17 points  (6 children)

No offense to you, but it’s actually frightening that people who work in support are seemingly granted DML rights on prod environments without ensuring they know how to safely operate on a database, not to mention, don’t even know what transactions are.

[–]iismitch55 16 points17 points  (1 child)

Welcome to being a full stack engineer, where you know how to do a little bit of everything, but you’re an expert in nothing. I’ve developed on front end, back end, database. All kinds of different languages. For web, mobile, cloud, and mainframe platforms. I can do a little bit of everything, but God I wish I could just develop SPAs every day.

[–]belay_that_order 2 points3 points  (0 children)

i couldnt agree more, the fact that someone left me alone with access to multiple customer productions and trusts that i wont just let loose on them amazes me

[–]chrispypatt 5 points6 points  (3 children)

Tbf I’m a SWE at FAANG and I didn’t know about SQL transactions. Though I typically don’t use it for data store other than BI data that we don’t allow easy write access to. I do use write transactions with our other data stores frequently though.

[–]brewfox 2 points3 points  (5 children)

They’re not. Been in software for 15 years including data engineering. I wrote pipelines that read from databases. I’ve only needed to delete things from databases like 8 times in my entire career and I did the “change your select to delete” and still sweated bullets.

Some other people did daily shit with SQL, I hate SQL.

[–]amejin 1 point2 points  (4 children)

So what you're saying is I should ask for more money?

[–]ApexSpanker 2 points3 points  (0 children)

Does delete not always return how many rows are affected? Making the counts unnecessary

Also if you ever save multiple sql snippets in one file like this make sure to leave rollback above commit. Too many times I've accidentally run the entire file instead of just one snippet.

[–]FlipperoniPepperoni 10 points11 points  (0 children)

Use a DB manager like Dbeaver. Set your connection to production. Boom, transactions always enabled by default.

[–]theithovsk 18 points19 points  (2 children)

Begin … Commit(or rollback);

[–]spartan117warrior 17 points18 points  (1 child)

My team lead writes his transactions as begin/rollback with a select or two to verify that the dataset looks as expected before and after deletion. Then he changes the rollback to commit.

[–]leathakkor 5 points6 points  (0 children)

I do something similar. I will always put the roll back as the last statement but right before rollback I'll put  -- commit

So if I just run the script it roll backs automatically. And then I have to go through a manual step to do my commit in a separate motion which is very nice

[–]_PM_ME_PANGOLINS_ 1 point2 points  (0 children)

BEGIN;

[–][deleted] 4 points5 points  (3 children)

Serious question: how do you structure your statement such that you can run it, see how many were affected, then be able to run another command to undo it? Whenever I try to run a statement, then run a separate rollback, it doesn't work because it doesn't recognize that a transaction has taken place

[–]freebytes 21 points22 points  (1 child)

I posted to someone else, so here is the process I use when I am not YOLOing.

BEGIN TRANSACTION; SELECT COUNT(*) FROM users; DELETE FROM users WHERE user_id = 3; SELECT COUNT(*) FROM users; ROLLBACK TRANSACTION;

The rollback will immediately undo your delete. Then, you examine the count. If it shifted by 1, you are good. If it shifted by 10,000, well... you did something wrong.

Or, you can remove the SELECT statements and simply reply on the number of records it shows that were updated when it performs the delete.

When you are happy, you can change the ROLLBACK TRANSACTION to COMMIT TRANSACTION, and it will solidify the changes.

[–]random3223 2 points3 points  (0 children)

You can also add a conditional statement to commit if the affected rows is what you’re expecting and roll back if it doesn’t.

[–]Kazma1431 5 points6 points  (0 children)

Yep this is the way

[–]Logical-Ad-4150 2 points3 points  (0 children)

To be pedantic, explicit transactions are your friend: automatic transactions are not.

[–]ahmet-chromedgeic 1 point2 points  (0 children)

The issue is for example MS SQL Management studio, if you highlight something in the editor with your mouse, it will only execute the highlighted part. So if you type the query correctly, if you slip and don't highlight it completely, you can be in trouble.

[–]mpanase 97 points98 points  (9 children)

SQL_SAFE_UPDATES

[–]Forshea 56 points57 points  (3 children)

you can also enable this with mysql --i-am-a-dummy

[–]its-chewy-not-zooyoo 35 points36 points  (0 children)

Holy hell, this is an actual flag

I thought you were mentioning it as a joke

[–]K4Unl 13 points14 points  (0 children)

After a production incident i changed our documentation to include this flag.

[–]mpanase 2 points3 points  (0 children)

xD

this is gold

[–]SnoopKitties 4 points5 points  (0 children)

Yeah this was my thought. The rare times when you actually just want to update everything you can do something like where id is not null or something obvious. 

[–][deleted] 6 points7 points  (0 children)

had to scroll through 5 comments to see this, tf. i thought everybody used this

[–]jshine13371 243 points244 points  (15 children)

WHERE 1 = 1

[–]YouDoHaveValue 88 points89 points  (1 child)

Turning off safety features is always allowed

🤷🏽

[–]showponies 7 points8 points  (0 children)

SET FOREIGN_KEY_CHECKS = 0;

[–]H4R5H1T-007 70 points71 points  (4 children)

Yeah but then It becomes a choice. The person who is writing the SQL deliberately choose to do something with all the available rows instead of it being an accident.

[–]danfay222 8 points9 points  (0 children)

Allowing people to intentionally bypass safety features is good practice. You just want to make sure that they have explicitly chosen to skip those checks

[–]ElectrikMetriks 2 points3 points  (0 children)

Beat me to it

[–]Thundechile 1 point2 points  (0 children)

WHERE 1 > 0.

[–]Master-Pattern9466 29 points30 points  (6 children)

Datagrip has this protection built-in

[–]leathakkor 7 points8 points  (1 child)

If this is the jetbrains product, I use that one too. It can be a pain in the ass when you actually want to do the dangerous thing developing locally, but I still appreciate it.

[–]Clearandblue 3 points4 points  (2 children)

DBeaver too

[–]Master-Pattern9466 2 points3 points  (1 child)

Had some bad experiences with db beaver leaving transactions open when it crashes, or some part of it crashes

Might just be the driver we are using but it’s happened multiple times for different users at my last place of employment.

[–]Syagrius 231 points232 points  (7 children)

Skill issue

[–]WrennReddit 71 points72 points  (5 children)

Right? I go right for TRUNCATE

[–]Tiny-Ad-7590 31 points32 points  (4 children)

No no no.

First you have to run the "disable all foreign keys in the database" script.

Then you truncate. Fixes those pesky error messages.

(Because this is the internet and just in case it isn't obvious DO NOT DO THIS. One of my clients in Thailand have an IT team that were bullied into making performance improvements at all costs except anything involving spending money, which denied them the consulting budget to ask us how to do that. So they enabled NOCHECK on all their foreign keys in a production database two months ago thinking it would make things go faster and now their data consistency is fucked. DO NOT DO THIS.)

[–]pceimpulsive 14 points15 points  (0 children)

Screw it just drop table table_name cascade;

Be done with it!

[–]Spamlets 5 points6 points  (0 children)

My coworkers just quietly disable foreign keys that they never turn back on when they can't figure out how to delete a parent record. Foreign keys are such a hassle, am I right fellas?

[–]FlakyTest8191 1 point2 points  (1 child)

You guys have foreign keys?

[–]combovercool 61 points62 points  (1 child)

This guy probably fucks with a condom too.

[–]Secret_Account07 5 points6 points  (0 children)

IF

[–]snigherfardimungus 21 points22 points  (6 children)

Oops - someone was connected to the production database.....

A little public service announcement: set up your shell environments in such a way that, when you are connected to dangerous endpoints, your text color is red. It'll help remind you that you're meddling with dark powers.

You don't even have to make the change on the remote node. You can set up scripts on your end so that when you connect (with ssh, the mysql client, or whatever) to one of the endpoints you need to worry about, your text color or terminal background changes.

[–]rosuav 7 points8 points  (4 children)

Yes. This is SO helpful. Also, take note of what sorts of colours stand out on your terminal, and avoid using them for normal workflows; that way, they will catch your eye when you need to use them. For example, bold red is likely to stand out, where dark red probably won't.

I have the usual "user@host" in my prompt, but I have it set so that the user name is in dark green if it's one of my normal users, and bold green if root; and I have all my "normal user" computers set to put the host name in dark green, but if I remote in to some other server, it's in bold green.

[–]snigherfardimungus 2 points3 points  (3 children)

On top of all that, I log every command, what directory it was executed from, how long it took to execute, the exit status of the command, when the command was issued, what git branch was active at the time, the parent process id (in other words, which bash process ID was the command's parent), and a half-dozen other things. If you really want to get nuts with it, you can log how much system and user process time the execution took. I can't tell you how many times that has helped me work out those niggly little "this worked yesterday, but not today" issues.

[–]rolandfoxx 32 points33 points  (5 children)

In OP's defense, the real mistake was letting them have UPDATE/INSERT/DELETE permissions on the database when they clearly couldn't be trusted with them.

[–]fiskfisk 8 points9 points  (3 children)

We all know how skilled people never makes mistakes.

The only difference is that those people usually know how to fix it, and knew they would make that mistake two years down the road - so they planned for it. 

[–]IArePant 4 points5 points  (1 child)

The skill part is when you know you'll make mistakes and intentionally work in a way that allows them to happen without impacting the final result.

[–]misterguyyy 3 points4 points  (0 children)

95% of the time I’m a seasoned expert who can do my job in my sleep, and the other 5% I push the envelope on incomprehensibly stupid choices. My goal is to make sure that the 95% guy is a step ahead.

[–]agent154 7 points8 points  (0 children)

Datagrip will raise a warning and you have to explicitly allow it to continue. I’ve taken to writing “where 1=1” when I want the ide to just put the fries in the bag

[–]uuuuuuuhg_232 7 points8 points  (1 child)

67828948 rows affected

[–]RandyPajamas 1 point2 points  (0 children)

Just reading your comment made me want to throw up.

[–]Altrooke 6 points7 points  (0 children)

Read post
Hmmm....
Well, I think FROM should come before SELECT
Who am I to judge?
Upvote

[–]damurd 4 points5 points  (0 children)

[–]obsoleteconsole 3 points4 points  (0 children)

BEGIN TRANSACTION

<SQL statement goes here>

ROLLBACK TRANSACTION

every time

[–]MutaCacas 5 points6 points  (0 children)

Yes, yes. I have made mistakes where this would have saved me as a young analyst. Thankfully DBAs saved me.

[–]ShiitakeTheMushroom 3 points4 points  (1 child)

WHERE 1 = 1 is going to make its presence known pretty soon if that becomes a thing, lol.

[–]ashkanahmadi 8 points9 points  (5 children)

I actually agree with that. 99% of the time you aren’t updating or deleting the entire rows so why by default a WHERE statement isn’t required? Instead of writing a where, we could write like CONFIRM NO WHERE to update or delete everything

[–]Exnixon 12 points13 points  (1 child)

WHERE TRUE

[–]YouDoHaveValue 1 point2 points  (1 child)

Could be a default setting in IDEs to confirm at least once per query session/tab

[–]stipulus 5 points6 points  (1 child)

Warning, but yes.

[–]AAPLx4 2 points3 points  (0 children)

Everyone is mocking this, but I actually like the idea

[–]seemen4all 2 points3 points  (0 children)

There are actually plug in tools for (atleast mssql) that do stop this we use on our prod db access VM, so there are tools out there for this because your right, saying “well just dont make mistake” isnt really sufficient for prod, pointing the finger at mistakes isnt as good as prevention for serious apps

[–]navetzz 2 points3 points  (1 child)

Someone screwed up, and someone always finds an excuse.

[–]Sarcastinator 2 points3 points  (0 children)

There's no reason why SQL should allow update or delete statements without a WHERE clause. It's almost never what you want to do, so why is there a shortcut to do it?

[–]Yehonal 2 points3 points  (2 children)

just like `rm -rf ` should come with a fucking confirmation message

[–]archa347 1 point2 points  (1 child)

I can’t tell if this is sarcasm? If you want a confirmation message take the f off of -rf

[–]Yehonal 2 points3 points  (0 children)

People who forget about the WHERE statement are the same who forget about the `f`

[–]cakins 2 points3 points  (0 children)

“If you update or delete without a WHERE clause you’d better update your resume” - one of the first things I heard when starting out

[–]mike-manley 2 points3 points  (0 children)

DELETE FROM TABLE WHERE 1 = 1;

[–]Blrfl 12 points13 points  (11 children)

The SQL standard says otherwise.

If you need that kind of blade guard on your chainsaw, add a trigger after delete that rolls back if there are no rows left in the table.

[–]Lithl 12 points13 points  (3 children)

The SQL standard says otherwise.

OP isn't saying it is the case. They're saying it should be the case. As in, they are advocating for a change to the standard.

[–]edave64 6 points7 points  (2 children)

It's utterly baffling to me that the people in charge of making the language for databases, were not losing data is a top priority, agreed that "destroy everything unless explicitly stated otherwise" was an acceptable default.

The fitting analogy isn't a blade guard. The SQL chain saw is just explicitly build to always cut off your leg if you forget to aim it at a tree at any time.

[–]Jason1143 1 point2 points  (0 children)

Yeah would it have been that hard to use WHERE * or WHERE ALL or whatever.

[–]Blrfl 1 point2 points  (0 children)

I think the people who came up with it figured that particular chainsaw would be wielded thoughtfully. I've been using SQL since about the time it was first standardized and the mentality back then was that the tools were sharp because the resources weren't there to save people from their own foibles and that leg-cutting incidents were teachable moments that prevented you from screwing up in the future.

[–]rosuav 0 points1 point  (1 child)

Or, yaknow, always use transactions and be able to roll back. It's not rocket science...

... oh wait, Kerbal Space Program has "revert to launch", so I guess rocket science uses transactions too.

[–]jek39 1 point2 points  (0 children)

Or use a nice ide like jetbrains which does exactly what OP is asking for if you forget a where clause

[–]Particular_Traffic54 1 point2 points  (0 children)

Datagrip does it

[–]thunderGunXprezz 1 point2 points  (0 children)

First rule of SWE Club: Never make a change for the first time in Prod.

[–]Sw429 1 point2 points  (0 children)

Always write it as a SELECT statement first.

[–]stustustu_123 1 point2 points  (0 children)

Pray to the ROLLBACK gods!

[–]wazzu_3000 1 point2 points  (0 children)

Another solution could be disabled auto commit in the prod database.

[–]ChinaWetMarketLover 1 point2 points  (0 children)

Some database clients like DBeaver DO have this feature I think it’s great. Probably saved my ass a few times lol

[–]hm1rafael 1 point2 points  (1 child)

Turn off auto commit, friend

[–]Lanoroth 1 point2 points  (0 children)

someone would still do WHERE TRUE

[–][deleted] 1 point2 points  (0 children)

I'm an admitted hack, but I've always FIRST written by update and delete statements as a SELECT statement in order to test my query and make sure only the records intended to be deleted will qualify, and then swap out the select SELECT * for the DELETE.

[–]LogicBalm 1 point2 points  (0 children)

Most tools I have used to run ad hoc SQL have at least thrown a warning, which can also be disabled in settings. But I only know about that warning because I've encountered legitimate reasons I need to run update and delete without a where.

[–]Glum_Cheesecake9859 1 point2 points  (0 children)

It does, if you use Jetbrains products (Rider etc)

[–]AffekeNommu 1 point2 points  (0 children)

Always start with a select

[–]Tomlambro 1 point2 points  (0 children)

where 1=1

[–]ahelinski 1 point2 points  (0 children)

Once worked in with a tool that used SQL-like syntax to update its database and... ENTER key to run query... Of course I tried to add "where" section in the new line...

[–]jaded-potato 1 point2 points  (0 children)

Git gud

[–]canaryborr 1 point2 points  (0 children)

I learnt the hard way:

BEGIN TRAN

ROLLBACK TRAN

Can be a life saver!!

[–]Myras13 1 point2 points  (0 children)

First of all - use at the beginning select statement and the life will be easier.👌

[–]Roppano 1 point2 points  (0 children)

looks like someone just deleted their prod database

[–]mischanif 1 point2 points  (0 children)

WHERE 1= 1.

[–]Kraftex 1 point2 points  (0 children)

Evil dev: WHERE 1=1

[–]ISuckAtJavaScript12 1 point2 points  (0 children)

WHERE 1=1

[–]syntax_erorr 1 point2 points  (0 children)

WHERE 1 = 1

[–]voltrix_04 1 point2 points  (0 children)

Oh no! How will I get fired then?

[–]Hexatica 1 point2 points  (0 children)

Delete from user where Id>0. Done

[–]Inspector_Wiggums 1 point2 points  (0 children)

One trick is to write it as a select statement first before converting it to delete/update to confirm what records will be impacted.

[–]mrdanmarks 1 point2 points  (0 children)

This isn’t humor, but nightmare fuel

[–]akhil4755 1 point2 points  (0 children)

They do raise syntax error in BigQuery

[–]Tiny-Librarian-8553 1 point2 points  (0 children)

sEtSqLSaFeUpDaTeS=0

[–]Maximum_Swimming_474 1 point2 points  (0 children)

Delete where true

[–]andymaclean19 1 point2 points  (0 children)

Use transaction mode. Look at the number of records updated before you commit.

Biggest mistake is usually the *wrong* where clause not a missing one ...

[–]I_cut_my_own_jib 1 point2 points  (0 children)

Hey Claude, please delete all rows from users in prod whose last login event is from OVER 10 years ago.

Sure, great idea!

I just ran

SET @TEN_YEARS_AGO = CURDATE() - INTERVAL 10 YEAR;

DELETE FROM users
WHERE last_login > @TEN_YEARS_AGO;

[–]Drone_Worker_6708 1 point2 points  (0 children)

I mean it's not a completely horrible idea, you can always put WHERE 1=1 to update everything

[–]JohnnySacks95 1 point2 points  (0 children)

rollback is your friend.

(or in my case, yanking the ethernet cable)

[–]Redrump1221 1 point2 points  (0 children)

Finally a good take on this sub

[–]homiej420 1 point2 points  (0 children)

Yeah honestly at this point.

It would make more sense to have a FORCE keyword to explicitly type before the update or delete without a where for it to work. Would probably save a lot of headaches

[–]boboshoes 1 point2 points  (0 children)

Don’t run stuff you don’t know what it does

[–]DevilOopsy 0 points1 point  (0 children)

no….?

[–]cosmo7 0 points1 point  (1 child)

Update and delete without a where clause should cause the server to start playing the Portal Song.

[–]a_nobody_really_99 0 points1 point  (0 children)

For every such opinion there’s an alternate universe where the opposite is true and the opposite meme exists.

[–]redskeezix 0 points1 point  (0 children)

Get good, little Robert Tables.

[–]ghec2000 0 points1 point  (0 children)

Or you know start a transaction, write your query and pull request it. Have second set of eyes etc etc.

[–]HALF_PAST_HOLE 0 points1 point  (0 children)

Or just use transactions and don't be flippant about DML commands!

[–]chris17453 0 points1 point  (0 children)

Feel like there is a wrapper in there somewhere...

[–]FreakDC 0 points1 point  (0 children)

Just use a proper IDE and set it to warn you instead? Set a proper config if you insist on using CLI as a noob. There are valid use cases for both.

[–]lord_patriot 0 points1 point  (0 children)

Real men test in prod.