(If the query returned the expected amount of affected rows)
COMMIT TRANSACTION
(If the query did not return the expected amount of affected rows)
ROLLBACK TRANSACTION
Note: I’ve been told before that this will lock the affected table(s) until the changes made are committed or rolled back, but after looking it up it looks like it depends on a lot of minor details. Just be careful if you use it in production.
You can also return * to see the changes, or add specific fields.
Like for example:
Begin;
Update users
Set first_name=‘John’
Where first_name=‘john’
Returning *;
Then your
Rollback;
Or
Commit;
So you’d see all rows you just updated. You can get fancy and do a self join and see the original and updated data if you want. I like to run an identifying query first, so I know hey I should see 87 rows updated or whatever.
Haven’t had any issues with table locks with this, but we use Postgres. YMMV.
If for example a client application is (accidentally) firing doubled requests to your API, you might get deadlocks in this case. Which is not bad per se, as you don’t want to conform to that behaviour. But it might also happen if you have two client applications with updates to the same resource (patching different fields for example), in that case you’re blocking one party so a retry mechanism in the client or server side might be a solution.
Just something we noticed a while ago when using transactions.
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmerhumor@lemmy.ml
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
Posts must be relevant to programming, programmers, or computer science.
No NSFW content.
Jokes must be in good taste. No hate speech, bigotry, etc.
In T-SQL:
BEGIN TRANSACTION
{query to update/delete records}
(If the query returned the expected amount of affected rows)
COMMIT TRANSACTION
(If the query did not return the expected amount of affected rows)
ROLLBACK TRANSACTION
Note: I’ve been told before that this will lock the affected table(s) until the changes made are committed or rolled back, but after looking it up it looks like it depends on a lot of minor details. Just be careful if you use it in production.
Lol why did I have to scroll so far to see ROLLBACK
Because this is c/programmerhumor and the OP hasn’t covered ROLLBACK yet in his sophomore DB class.
Transactions are the safe way of doing it.
You can also return * to see the changes, or add specific fields.
Like for example:
Begin; Update users Set first_name=‘John’ Where first_name=‘john’ Returning *;
Then your Rollback; Or Commit;
So you’d see all rows you just updated. You can get fancy and do a self join and see the original and updated data if you want. I like to run an identifying query first, so I know hey I should see 87 rows updated or whatever.
Haven’t had any issues with table locks with this, but we use Postgres. YMMV.
If for example a client application is (accidentally) firing doubled requests to your API, you might get deadlocks in this case. Which is not bad per se, as you don’t want to conform to that behaviour. But it might also happen if you have two client applications with updates to the same resource (patching different fields for example), in that case you’re blocking one party so a retry mechanism in the client or server side might be a solution.
Just something we noticed a while ago when using transactions.