I need to insert data into two separate tables in the same database. I’m currently using a separate query/transaction for each insert, but I recently learned that I can combine the two inserts into a single transaction.
What are the differences in combining them into one transaction instead of keeping them as separate transactions?
If the data are correlated, which means that the data stored by the second sql depend, are linked in some way with the data which are stored by the first query and/or vice-versa, then you should consider to make a
transaction. In such this way, you couldrollbackif one of the queries encounter a problem. You can implement this in atry-catchstatement. If the queries are totally unrelated each-other, you can go with two separate SQL statements.However I think that, as the data are the same for the two tables, it would be better to do a transaction. So, if you’ll have to make operations like joins, deletes, etc. in the future, you have a consistent database yet.