以原子方式更改 mySQL 中的多行
我有一个 mySQL 表,其形式为
entryID (PK)、UserID、entryName
每个用户(由其 userID 定义)可以在此表中创建任意数量的条目,但对于每个用户,entryName 必须是独一无二的。我想允许用户立即修改所有的entryNames。因此,我将向用户呈现一个包含多个可编辑的 EntryName 字段的表单。
问题在于尝试将其提交到数据库时。我不能像这样逐行更新:
1, 1, Entry1
2, 1, Entry2
尝试将 Entry1 重命名为 Entry2 时会
1, 1, Entry2
2, 1, Entry3
导致错误。目前,我读取具有给定 UserID 的所有行,然后删除它们,并重新创建每一行。这有效。然而,该方法的问题在于,如果用户设法在我的脚本中引起错误,他的所有条目都会被删除......并丢失。这是一件坏事。如何解决这个问题呢?
I have a mySQL table of the form
entryID (PK), UserID, entryName
Each user (as defined by his userID) may create any number of entries in this table, but for each user, the entryName must be unique. I would like to allow users to modify all of their entryNames at once. As such, I will present the user with a form with multiple entryName fields which they can edit.
The trouble is when trying to commit this to the database. I can't just update row by row as the case:
1, 1, Entry1
2, 1, Entry2
becoming
1, 1, Entry2
2, 1, Entry3
would cause an error when trying to rename Entry1 to Entry2. Currently, I read all rows with the given UserID, then delete them, and recreate each row. This works. The trouble with that method, however, is that if a user manages to cause an error in my script, ALL of his entries get deleted... and lost. And this is a Bad Thing. How would one solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是异常时回滚事务的典型场景。
您可以从这里开始: http://dev.mysql.com/doc/ refman/5.0/en/commit.html
This is a typical scenario for a TRANSACTION WITH ROLLBACK on EXCEPTION.
You can start here: http://dev.mysql.com/doc/refman/5.0/en/commit.html