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.

Ask HN: Best Practices to run update queries on production DB

3 pointsby xeonaxover 1 year ago
I am in a new role where I would be sometimes (rarely) required to run update queries on a production DBs. The Tables mostly have 1000 to 10000 rows.<p>I might be required to update 1 to few rows those times. I am not a SQL expert, but know a bit of it.<p>Generally I expect, I would need to create a SELECT query first to identify the rows that I need to modify. SELECT query should exactly return the rows that need modifications, nothing more.<p>Then create an update query using the where clause above. I was thinking of wrapping the update query in transaction and running the statements sequentially, while keeping track of no of rows modified, as well confirming the changes using SELECT query. like below:<p>BEGIN TRANSACTION t1; UPDATE tablename SET attr1=&#x27;attr1&#x27;, attr2=&#x27;attr2&#x27; WHERE Attr3=&#x27;attr3&#x27; AND ID=&#x27;10&#x27;; -- At this point no of rows modified should match the number of rows returned by our previous SELECT query SELECT * from tablename where WHERE Attr3=&#x27;attr3&#x27; AND ID=&#x27;10&#x27;; -- Expected modification should be seen here. COMMIT TRANSACTION t1;-- Commit if everything OK till here.<p>I will be using Azure SQL Database Query Editor to run the queries step by step.<p>What else do I need to be mindful about? I don&#x27;t want to be the person who dropped entire customers table.

3 comments

brodouevencodeover 1 year ago
Something to think about: <a href="https:&#x2F;&#x2F;microservices.io&#x2F;patterns&#x2F;data&#x2F;cqrs.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;microservices.io&#x2F;patterns&#x2F;data&#x2F;cqrs.html</a>
pestatijeover 1 year ago
Best practice is not to run update queries on production DB<p>Too many things can go wrong...in fact nobody should have permissions to do so...if you think there is a danger the entire customers table might be dropped, backup the DB
PaulHouleover 1 year ago
Have a backup just in case.