我什么时候应该在查询中使用事务?

发布于 2024-08-19 05:12:25 字数 430 浏览 4 评论 0原文

我正在阅读有关如何使用数据库类型和数据库引擎的事务的非常详细的教程,但我还没有找到指导我何时以及为何应该使用它们的指南。

我知道交易通常用于银行业务,因此当我们处理货币数据时,但我可以想象它们还有许多其他用途。

今天,我正在开发一个包含关系数据库的各种 INSERT 语句的页面,我想知道这是否是我应该使用它们的情况之一。

我的印象是,我不知道数据可能部分丢失的情况(除了编码器错误),所以我总是担心何时应该使用它们。

有人可以解释这些基本规则或提供一些与这些基本规则的链接吗?

我正在使用 MySQL 5.0.8。我应该对所有需要事务的表使用 InnoDB 吗?如果是,InnoDB 是否比常见的 MyISAM 慢,但我不应该担心这一点?

谢谢

I'm reading very detailed tutorials on how to use transactions with database types and database engines, but I haven't found a guide that teaches me when and why I should use them.

I know transactions are usually used for banking, so when we work with money data, but I can imagine they are used in many other ways.

Today I'm working on a page with various INSERT statements for a relational database, and I wanted to know if this is one of the cases when I should use them.

I get an impression that I don't know the cases when the data can be partially lost (apart from coder errors) so I'm always worried about when I should use them.

Can someone explain or give some link with these fundamental rules?

I'm using MySQL 5.0.8. Should I use InnoDB for all tables that need transactions? If yes, is InnoDB slower than the common MyISAM but I shouldn't worry about that?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

怎樣才叫好 2024-08-26 05:12:25

基本上,任何时候您的工作单元对外部更改敏感,或者在发生错误或其他原因时需要能够回滚每个更改

在此处查找一些出色的答案及其使用原因

Basically any time you have a unit of work that is either sensitive to outside changes or needs the ability to rollback every change, if an error occurs or some other reason.

Look here for some excellent answers and their reasons for using them.

治碍 2024-08-26 05:12:25

除了 Nick Craver 所写的之外,当您有一系列需要原子执行的写入操作时,您可能还需要使用事务;也就是说,它们应该全部成功,或者没有一个成功。

In addition to what Nick Craver wrote, you would want to use a transaction when you have a series of writes that need to be performed atomically; that is, they should all succeed or none should succeed.

岁吢 2024-08-26 05:12:25

当任务无法完成或其他人在任务中间读取或写入可能导致数据损坏时,应使用事务。这些包括但不限于:

  • 从表中读取以便后续删除
  • 将相关数据写入多个表

Transactions should be used when there is the possibility that either failure to complete or someone else reading or writing in the middle of your task could cause damage to the data. These include but are not limited to:

  • Reading from a table for subsequent deletion
  • Writing related data to multiple tables
旧梦荧光笔 2024-08-26 05:12:25

当您有一组必须是原子的操作(全部成功或全部成功)时,您可以使用事务。将这些操作包装在事务中允许您在遇到错误时回滚已经成功的操作。它还确保您正在使用的数据是一致的,因为锁定将一直保持到事务完成为止。

You use transactions when you have a group of actions that must be atomic (either all succeed or none succeed) Wrapping these actions in a transaction allows you to rollback actions that have already succeeded when you encounter an error. It also ensures that the data you are working with is consistent as locks will be held until the transaction is complete.

傲性难收 2024-08-26 05:12:25

在某些框架中,例如Spring,自动事务允许在事务失败时重新执行事务。

In some frameworks, e.g. Spring, automatic transactions allow to re-execute a transaction if it failed.

指尖凝香 2024-08-26 05:12:25

主要用于多个相同的凝乳案例,您希望保存对有效条目的更改,对于这些表单提交案例的每个步骤具有多个输入的多步骤表单,以及在获得外部 api 反馈后提交表单。主要是当您想要时回滚您的查询以获得某些返回值。

Mostly use for multiple same curd cases, where you want save changes for valid entries, for a multi steps form with multiple input for each step of those form s submit cases, and form submit after getting an external api feedback.. mainly when you want rollback your query for some return value.

你没皮卡萌 2024-08-26 05:12:25

当手动处理敏感数据库时,使用事务作为安全/验证步骤来验证对表所做的更改是否正确且符合预期也是一个好主意。这可以避免您错误地保存错误数据的麻烦。

示例

我更新表 A 并将字段 x 设置为错误值,无意中覆盖了之前的正确值。

如果我在事务中,我将能够选择我的行并检查我的更改,发现错误并回滚。如果我的行看起来正确,那么我会提交

如果我不在事务中,我就会丢失正确的值。

When fiddling with a sensitive database manually, it could be also a good idea to use a transaction as a safety / validation step to validate that the changes you made to a table were correct and as intended. This could save you the trouble of saving bad data by mistake.

Example:

I update table A and set field x to a wrong value unintentionally overriding the previous correct value.

If I were inside a transaction I would be able to select my row and check my change, spot the error and rollback. If my row looked correct then I would commit

If I were not inside a transaction, I would have lost the correct value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文