MySQL 更新“类别”;链接表

发布于 2024-09-06 15:06:38 字数 254 浏览 1 评论 0原文

我有一个表,其中包含参考书目条目,并带有 bibID 主键。我还有一个表,其中包含可以使用categoryID 主键分配给参考书目条目的类别列表。一个表将这两个表链接为bibID:categoryID,以便每个bibID可以与多个categoryID关联。

与参考书目条目相关的类别可以通过带有代表所有可能类别的复选框的表单进行编辑。

更新这种关系最有效的方法是什么?我可以从与条目关联的链接表中删除所有关系,然后重新插入表单中显示的任何内容,但这似乎效率低下。

I have a table that holds bibliography entries, with a bibID primary key. I also have a table that holds a list of categories that can be assigned to the bibliography entries with a categoryID primary key. A table links these two tables as bibID:categoryID, so that each bibID can be associated with multiple categoryIDs.

Categories associated with bibliography entries can be edited via a form with checkboxes that represent all possible categories.

What is the most efficient way to update this relationship? I could just delete all relationships from the linking table associated with an entry and then reinsert whatever the form says, but this seems inefficient.

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

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

发布评论

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

评论(1

眼前雾蒙蒙 2024-09-13 15:06:38

效率是一个难以捉摸的术语。它对不同的人来说可能意味着不同的事情。

然而,在大多数情况下,它意味着“性能”,所以我假设这就是您现在的意思。

我怀疑现实情况是,这是最有效(性能)的方式。

其他方法可能看起来更优雅,因为它们将保留现有数据,并且仅添加丢失的数据,但它们将(可能)需要更多的数据库访问和(肯定)更复杂的 SQL。一次删除数据库调用和一次添加数据库调用应该可以解决您的问题。

唯一的例外可能是条目数量较多且更改较小(或可以忽略不计)。在这种情况下,您可能需要重新考虑。

Efficiency is a slippery term. It can mean different things to different people.

However in most cases it means "performance", so I will assume that is what you mean for now.

I suspect the reality is that this is the most efficient (performant) way.

Other methods may appear more elegant, as they will preserve existing data, and only add missing data, but they will (potentially) require more database accesses and (definitely) more complicated SQL. One database call to delete and one to add should fix you up.

The only exception may be where there are large numbers of entries and the changes are small (or negligible). In this case you may need to reconsider.

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