简体   繁体   English

更新具有特定条件的行时是否需要事务

[英]Do I need a transaction when updating a row with a specific condition

I am working on a small project and I want to flag some items in a table as "claimed".我正在做一个小项目,我想将表格中的某些项目标记为“已声明”。

So most items would have a claim_id = NULL, when I want to claim an item I will call this command to update the next available item:所以大多数物品都会有一个claim_id = NULL,当我想认领一个物品时,我会调用这个命令来更新下一个可用的物品:

update ci_items
set claim_id = ?
where claim_id IS NULL 
order by id 
LIMIT 1;

I think this should always either update the next available item, or nothing if all items have been claimed.我认为这应该总是更新下一个可用的项目,或者如果所有项目都已被认领,则什么都不更新。 In the context of a web application where 2 db connections could run this at the same time, is there any risk that an item gets claimed twice at the same time, one claim_id overriding the previous one?在 web 应用程序的上下文中,2 个 db 连接可以同时运行,是否有任何风险同时声明一个项目两次,一个 claim_id 覆盖前一个?

I am thinking I could use a transaction but the table structure is currently MyISAM and I don't think that supports transactions我想我可以使用事务,但表结构目前是 MyISAM,我认为不支持事务

What you have show us works well.您向我们展示的内容效果很好。 Individual SQL update queries have an implicit transaction (also known as "autocommit") for their duration.单个 SQL 更新查询在其持续时间内具有隐式事务(也称为“自动提交”)。

Go for it. Go 为它。

And don't stop asking yourself this kind of Atomicity, Consistency, Isolation, Durability ( ACID ) question as you design and build your application.在设计和构建应用程序时,不要停止问自己这种原子性、一致性、隔离性、持久性 ( ACID ) 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要在这里交易吗? - Do I need a transaction here? 在Laravel中批量更新,每行具有特定的where条件 - Bulk updating in Laravel with specific where condition for each row 更新数据时获取特定行 - fetching a specific row when updating a data 我需要根据特定条件将值推入数组,执行此操作的正确方法是什么? - I need to push into an array values based on a specific condition, what is the correct way to do this? 更新行时,如何通过Laravel表单请求验证现有行是否包含值? - When updating a row, how do I validate that the existing row contains a value with Laravel form requests? 更新数据库中的记录,仅在需要更改时更改所有行 - Updating record in database, changes all when need it to change only 1 row 当它们满足条件时,如何遍历while循环和行变量? - How do I loop through the while loop and through row variables when they meet a condition? 我需要帮助来更新特定用户的sql条目 - I need help updating sql entry for a specific user 我是否需要(我将需要)锁定共享模式| 在交易中进行更新? - Do I need (will I ever need) LOCK IN SHARED MODE | FOR UPDATE within a transaction? 当我点击表格表格中的特定行时,如何根据ID调出? - How do i call out according to the id when i click on specific row in my table form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM