由于“重复条目",删除后的mysql插入失败; [英] mysql insert after delete fails because of "duplicate entry"
问题描述
我有一个包含两个 mysql 查询的代码.DELETE FROM my_table WHERE user_id=some_number
INSERT INTO my_table (user_id, ... ) VALUES(some_number, ...)
I have a code with two mysql queries.
DELETE FROM my_table WHERE user_id=some_number
INSERT INTO my_table (user_id, ... ) VALUES(some_number, ...)
字段 user_id 是唯一的.
The field user_id is unique.
在极少数情况下,插入失败,声称发生了重复的条目.我的第一直觉让我相信 DELETE 没有完成,现在插入正在尝试插入,我得到了一个重复的条目.这可能吗?我怎样才能避免这种情况?你能想到不同的解释吗?
In rare cases the insert fails claiming a duplicate entry occurred. My first instinct leads me to to believe the DELETE didn't finish and now the insert is trying to insert and I'm getting a duplicate entry. Is this possible? How can I avoid this? Might there be a different explanation you can think of?
更新:我删除的原因是因为我想删除所有我不是第一次更新/插入的数据.此外,我认为重要的是要声明大部分数据保持不变.
Update: The reason I'm deleting is because I want to delete all the data that I am not updating / inserting for the first time. Also, I think it is important to state that most of the data remains the same.
推荐答案
SET AUTOCOMMIT=0;
START TRANSACTION;
DELETE FROM my_table WHERE user_id=some_number;
INSERT INTO my_table (user_id, ... ) VALUES(some_number, ...);
commit;
这篇关于由于“重复条目",删除后的mysql插入失败;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!