[英]Updating existing records in the database due to changes?
Database tables: 数据库表:
task_assignee task_assignee
------------------------------
| id | task_id | assignee_id |
------------------------------
| 1 | 3 | 2 |
| 2 | 3 | 5 |
task 任务
---------------------------
| id | content | category |
---------------------------
| 3 | 'Bake' | 'A' |
Here is the scenario: 这是场景:
task_assignee
for that task. 这等于该任务在task_assignee
两条记录。 I save the task. 我保存任务。 What would be the most efficient way to make these changes to the database? 对数据库进行这些更改的最有效方法是什么?
I'm thinking of adding another column to task_assignee
that will serve as a flag of sorts that I will update. 我正在考虑向task_assignee
添加另一列,该列将用作我将更新的各种标志。 I will set it to 0 if the assignee is unselected, 1 if it is selected, but I'm not sure if that'd be a good thing to do, and I don't think that can cover when new assignees are selected. 如果未选择受让人,则将其设置为0,如果未选择,将其设置为1,但是我不确定这是否是一件好事,而且我认为选择新的受让人时不能涵盖所有情况。 What I can think of is a tedious loop that will check if the new assignee selected for that task is already in task_assignee
and if it is already attached to that task, if it isn't then it inserts a new one. 我能想到的是一个繁琐的循环,它将检查为该任务选择的新受让人是否已经在task_assignee
并且是否已经附加到该任务,如果不是,则插入新任务。
I also thought of just deleting the past records first then insert the new ones, but I'm not sure if that's efficient? 我还考虑过先删除过去的记录,然后再插入新的记录,但是我不确定这是否有效? -> When adding a new assignee ->添加新的受让人时
Isn't it just more simple to delete the record in task_assignee from the user you want to remove from the task and add the one you want to add. 从要从任务中删除的用户中删除task_assignee中的记录,然后添加要添加的记录,不是更简单。 So you only need two queries. 因此,您只需要两个查询。 Delete from and insert into. 从中删除并插入。
You should keep the tables as clean as possible and since the task_assignee table has no unique id you can delete all the records you don't need. 您应该保持表的整洁,并且由于task_assignee表没有唯一的ID,因此您可以删除所有不需要的记录。
As said above you can also do the update method. 如上所述,您也可以执行更新方法。 I'm more a fan of deleting and adding it in again. 我更喜欢删除并再次添加它。 Just a force of habit. 只是一种习惯。
kind regards 亲切的问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.