使用三列标记数据库架构中的同义词
假设您具有与此处所述相同的数据模型。
Table: Item
Columns: ItemID, Title, Content
Table: Tag
Columns: TagID, Title
Table: ItemTag
Columns: ItemID, TagID
如果您希望拥有与 stackoverflow
上类似的标签同义词,您会将此信息放置在数据模型中的什么位置?您会只添加一个带有逗号分隔的同义词列表的属性,还是会稍微转换一下数据模型?
用于插入新项目的 SQL
语句是什么样的?新项目将与所有同义词连接还是仅与一个主标签连接,并且在代码中我们必须检测这是一个同义词?
您建议采用哪种方法来顺利进行插入、删除和按标签搜索操作?
Assume you have the same data model as described here.
Table: Item
Columns: ItemID, Title, Content
Table: Tag
Columns: TagID, Title
Table: ItemTag
Columns: ItemID, TagID
If you want to have tag synonyms similarly as are on stackoverflow
, where would you place this information in the data model? Would you add only one attribute with comma-separated list of synonyms or would you transform a data model a little?
How the SQL
statement for inserting new item would look like? Will the new item be connected with all the synonyms or only with one main tag and in the code we have to detect that this is a synonym?
Which approach would you recommend for smooth inserting, deleting and searching by tag operations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
Tag
表并添加TagSynonym
表:TagSynonym.TagID
将是Tag(TagID)
的 FK > 和Title
将是 PK。如果您还想拥有“MainSynonym”标签,那么您可以使用以下代码:
TagMainSynonym(TagID)
为 PK,TagMainSynonym(TagID, Title)
为 FK到TagSynonym(TagID, Title)
。它可能看起来已经过期,但像合并两个标签这样的简单操作只需要在
Tag.TagID
上进行简单的UPDATE
(一行),而级联效果将完成其余的工作(在 3 个表中)Change the
Tag
table and add aTagSynonym
table:The
TagSynonym.TagID
would be an FK toTag(TagID)
and theTitle
would be the PK.If you also want to have a "MainSynonym" tag, then you could use this:
with
TagMainSynonym(TagID)
being the PK andTagMainSynonym(TagID, Title)
being an FK toTagSynonym(TagID, Title)
.It may look overdue but a simple operation like merging two tags will only need a simple
UPDATE
(one row) on theTag.TagID
and the cascading effects will do the rest (in 3 tables)