How to insert/update multiple-tables in a single transaction?


Using multiple tables in a single transaction is common enough. You'd want the data to be either inserted/updated in all of them, or in none of them. So if one of such DML operations fail, then the others should not succeed to.

In other words, your work should be either all committed, or all rollbacked.

This example is a weather application that records temperatures for any given zip code. And it updates the climate (temperature) averages for each code too. So in such a transaction, one table is inserted and other updated, and that's done atomically.

There's one example each of the database:
Of course, they are very similar, with minor differences in SQL; however database setup is different so each is in a separate example. That way it's easy to follow.


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.