TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

An UPDATE without a WHERE, or something close to it

91 pointsby l0b0over 3 years ago

22 comments

jusssiover 3 years ago
A particularly nasty aspect of SQL UPDATE syntax is the predicate order, i.e. SET is before WHERE. So when you type it in order, there&#x27;s a dangerous phase you have to go through if you hit enter by accident.<p>Having run an `mkfs.ext3 &#x2F;dev&#x2F;sda` (note the missing partition number) by accident, I&#x27;ve learned to start potentially destructive commands by typing first whatever makes that command a comment (# in shell, -- in sql), then going back to the start of the line to remove the safety when I&#x27;m done.
评论 #29043189 未加载
评论 #29038281 未加载
评论 #29042507 未加载
评论 #29039339 未加载
评论 #29042954 未加载
评论 #29045472 未加载
评论 #29039320 未加载
评论 #29038276 未加载
评论 #29043162 未加载
评论 #29046410 未加载
评论 #29038314 未加载
评论 #29043173 未加载
评论 #29038447 未加载
blakesterzover 3 years ago
Waaay back in the .com boom days of the late 90s I was working at a place where the CTO did something like:<p>UPDATE users SET password = &#x27;23r23r23rdsf&#x27;;<p>Somehow he missed the &quot;WHERE email = &#x27;someone&#x27;;&quot; I forget exactly how many users had their password changed that day. For some reason (maybe MySQL didn&#x27;t let you cancel an UPDATE like that way back in the old days?) to stop that query he ran into the other room and unplugged the MySQL server. The PROD server. It&#x27;s funny to look back on that now, but at the time... oh boy, total panic.
评论 #29038142 未加载
评论 #29038171 未加载
评论 #29038146 未加载
评论 #29040862 未加载
评论 #29038598 未加载
评论 #29042615 未加载
jerfover 3 years ago
There is certainly a tension in modern devops tools, between the desire to do things easily across large sets of machines (and especially across large sets of <i>diverse</i> machines) easily, and the fact that you lose all the <i>advantages</i> of inertia and difficulty in making stupid changes the better you get at that. As you scale up this tension gets worse and worse. You don&#x27;t really want to create a tool that allows you to trash &quot;every router you have&quot; in one fell swoop, or to trash &quot;every server in every data center&quot;, and yet at the same time, you <i>need</i> the ability to manipulate &quot;every server&quot; at the same time with the same tool because who can afford to log in and manually change 20,000 machines? It&#x27;s really difficult to have &quot;the power to administer&quot; 20,000 machines without having &quot;the power to destroy&quot; them all.<p>You can&#x27;t even easily ask &quot;are you sure?&quot; because if you&#x27;re asking that for every little thing it ceases to be a useful guard. You need tools that detect if you&#x27;re doing something stupid and dangerous and only ask then, but in the limit, that&#x27;s strong-AI hard for ops people. That is, there are some obvious ones you can try to catch... &quot;did you really mean to unassign all IP addresses?&quot;, but in general there&#x27;s always something that will go wrong more cleverly than your detection code.<p>Hooking machines up to orchestration code is something I have to do. I operate at scales that Facebook would laugh at, but they&#x27;re <i>still</i> well beyond what is practical to manually manage. In my opinion that scale taps out somewhere in the large single digits per ops person, which is nothing nowadays. But it always makes me nervous to do so, too, because I can see I&#x27;m putting all my eggs in one basket in the process, and the traditional &quot;watch that basket really hard!&quot; answer for when you&#x27;re stuck in that situation is visibly not adequate.<p>I don&#x27;t have a solution to propose. The tension seems fundamental to me. All I can suggest is that everyone sitting in front of any devops tool always be keeping the possibilities in mind, despite your brain&#x27;s desires to say &quot;hey, the last 1000 deploys went fine, I can stop being so vigilant about this one&quot;, and that any guard rails that can be added should be, even though they can never be 100% effective.
评论 #29038890 未加载
评论 #29038160 未加载
评论 #29041058 未加载
评论 #29039005 未加载
评论 #29040270 未加载
评论 #29038137 未加载
unfuncoover 3 years ago
I&#x27;ve done this on a MySQL database in production back in 2012, on a customer table, for a website that has been mentioned on HN a total of 22 times (not huge but some here have an interest) – the head of technology quickly put up a maintenance page and used the point in time restore feature in RDS and there was no damage, I didn&#x27;t get in trouble, we were back fully-functional within an hour.<p>Haven&#x27;t used MySQL in a while, but when I was using I&#x27;d have this alias in my zshrc:<p><pre><code> alias mysql=&quot;mysql --i-am-a-dummy&quot; </code></pre> It hasn&#x27;t happened since.
评论 #29042473 未加载
willciprianoover 3 years ago
When working in prod, first I write a select to grab all of the data I wish to modify, confirm that is correct, then add the set portion, then add the UPDATE last. Never had a issue and I work in prod all the time.
评论 #29042219 未加载
matsemannover 3 years ago
I made a feature recently that had an interface where one could change some configurations. And other stuff would link to a specific configuration. Mostly a configuration is only used one or two places, but nothing stops someone from reusing it for lots of items, so of course someone do.<p>When I thought I was done with the page for editing configurations, the UX person said it missed a popup confirming the config changes if it affected more than X items.<p>I&#x27;d prefer skipping it. It&#x27;s more state to hold, data to be fetched up front etc. But it has probably saved us multiple times already from someone trying to change something used lots of places by accident (instead of making a new separate config for whatever they want to change).
IceDaneover 3 years ago
Speaking of insane defaults: I&#x27;ve been working with azure for my latest project at work, using azure app services, which allow you to create &quot;deployment slots&quot; for different deployments(dev, staging, prod or just other customers).<p>When dealing with azure app services on the command line, you specify the slot with the `-s &lt;slot&gt;` flag.. but if you don&#x27;t, it defaults to the production slot.<p>I&#x27;m really not sure what kind of moron though that was a good default, because the az CLI doesn&#x27;t ask you for confirmation for anything. If you run `az webapp restart` you just restarted the production system.
eliover 3 years ago
In BigQuery, all UPDATE queries require a WHERE. &quot;WHERE TRUE&quot; is the recommended workaround if you really want to set a value everywhere. Makes a lot of sense!
RenThrayskover 3 years ago
With databases, start a transaction first, then update, and look at the number of rows affected. If it&#x27;s an oh shit moment, rollback, if not commit.
评论 #29039168 未加载
评论 #29038378 未加载
评论 #29038984 未加载
codeulikeover 3 years ago
re: UPDATE without a WHERE<p>In SSMS (the main query console tool thing for Sql Server) you can highlight a bit of code with the cursor (as if you want to copy&#x2F;paste it) and press CTRL-E to execute it, its really handy when you&#x27;ve got a big sketchpad-like series of SELECT statements and you&#x27;re doing exploratory tinkering.<p>But if your UPDATE statement is accross three lines, its a little bit too easy to accidentally select just the first two lines and not select the third line that has the WHERE clause. Then CTRL-E and you&#x27;ve footgunned yourself.<p>Its never actually happened to me, but I&#x27;ve often thought it probably has happened to some people.
评论 #29042876 未加载
评论 #29045268 未加载
评论 #29038530 未加载
评论 #29042964 未加载
ts330over 3 years ago
Surely this will become a non-problem as we migrate to NOSQL... no...? oh? we&#x27;re migrating back? great.
评论 #29039202 未加载
replerover 3 years ago
This is all well and fine, but if you&#x27;re going to go commando and update a production system by hand you should have a backup plan in mind before you start.<p>With SQL, that could be as simple as starting a transaction, doing your commando stuff, and committing it when you are satisfied.<p>But don&#x27;t do that. Why are you doing anything like that in production. Why why why.
markstosover 3 years ago
Ansible playbooks are often written to update all the servers in a group unless you explicitly to tell it to limit it only to a sub-set:<p><a href="https:&#x2F;&#x2F;docs.ansible.com&#x2F;ansible&#x2F;latest&#x2F;user_guide&#x2F;intro_patterns.html" rel="nofollow">https:&#x2F;&#x2F;docs.ansible.com&#x2F;ansible&#x2F;latest&#x2F;user_guide&#x2F;intro_pat...</a>
zoomablemindover 3 years ago
SQL operates on sets of records, so it&#x27;s up to the user to specify and restrict the set.<p>One can form a set by specifying just a table, or tune more by JOINs and further with WHERE clause.<p>Well, of course a JOIN is just one way to say WHERE. Still, when properly joined, the resulting set may not need a WHERE in UPDATE.
ts330over 3 years ago
Who are all you crack-junkies, running SQL queries against live...? I&#x27;ve seen enough literature saying we don&#x27;t do this anymore... ooooooooohhhhhh. right... yes, the people writing about that aren&#x27;t the ones actually shipping and making the clock tick ;-)
lowbloodsugarover 3 years ago
If this is a PROD database, you are running a script, not typing it into a console, and that script has been tested in PRE, and reviewed by others.<p>Edit: Gah, what have I become! Listen to me! I am so old. Fuck it, YOLO right? Oh, wait, I&#x27;ve got kids in college.
bob1029over 3 years ago
If you put a small program&#x2F;interface around SQL entry, you can trivially verify that a user entered a reasonable query before allowing to execute.<p>I think validation that a query is going to do what you want is up to the user of the database, not the database vendor.
评论 #29038497 未加载
jbverschoorover 3 years ago
If you use a tool that allows you &quot;execute editor&quot; and &quot;execute selection&quot;, you might just have selected the update part without the where, or accidentally press return so it executes as two separate statements.<p>Happened to me 18 years ago. :-)
tyingqover 3 years ago
UPDATE also supports ORDER BY and LIMIT, which is kind of odd when you first see it.
评论 #29038463 未加载
yrgulationover 3 years ago
in my early days of SQL i used to type in limit 1; and then go back to the beginning of the line and write the delete or update statement. Overkill but kept me safe from unwanted updates :) later on i started doing a select count followed by a limit to a number of records i knew should be updated. cant be too careful.
LanceHover 3 years ago
I have once typed &quot;ROLLBACK&quot; in order to get my heart started again -- I had updated the users table.
shimonabiover 3 years ago
This has never happened to me.<p>A SQL statement without a WHERE looks too suspicious to me to make a mistake like that.