单笔交易与多笔交易 [英] Single Transaction vs Multiple Transactions

查看:75
本文介绍了单笔交易与多笔交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据插入同一数据库中的两个单独的表中。我目前对每个插入使用单独的查询/事务,但是最近我了解到可以将两个插入合并到一个事务中。

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.

推荐答案

如果数据是关联的,则意味着数据已存储通过第二个sql依赖,以某种方式与第一个查询存储的数据链接和/或反之亦然,那么您应该考虑进行交易。这样,如果其中一个查询遇到问题,您可以回滚。您可以在 try-catch 语句中实现这一点。如果查询彼此之间完全不相关,则可以使用两个单独的SQL语句。

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 could rollback if one of the queries encounter a problem. You can implement this in a try-catch statement. 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.

这篇关于单笔交易与多笔交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆