存储分层标签的最佳方式

发布于 2024-11-15 04:02:49 字数 263 浏览 2 评论 0原文

我有一个列表,其中每个列表条目都标有多个标签。每个标签还可以有子标签。列表中的每个条目可以有多个标签。

例如,谈论汽车的列表条目可以具有名为“汽车”、“车辆”、“法拉利”的标签。

我应该能够查看标签的层次结构,如下所示。此外,每个条目的标签数量以及标签的深度不应受到限制。

我如何存储这些数据?我愿意使用任何类型的 DBMS。

在此处输入图像描述

I have a list, where each list entry is tagged with multiple tags. Each tag can also have child tags. Every entry in the list can have more than one tags.

For example, a list entry that talks about cars can have tags called "cars", "vehicles", "ferrari".

I should be able to view a hierarchy of tags, like shown below. Also, there should be no limit to number of tags per entry, and also how deep the tags can go.

How do I store this data? I am open to using any type of DBMS.

enter image description here

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

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

发布评论

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

评论(6

温柔少女心 2024-11-22 04:02:49

最简单的方法是使用父/子解决方案,但使用此数据模型编写有效的查询非常困难。

在 MySQL 中管理分层数据 是一篇关于分层数据结构的非常好的文章。我想其中大部分也可以应用于其他数据库系统。

The naive approach would be a parent / child solution, but it's very difficult to write efficient queries with this data model.

Managing Hierarchical Data in MySQL is a pretty good article about hierarchical data structures. I suppose most of it can be applied to other database systems, too.

铁轨上的流浪者 2024-11-22 04:02:49

我认为这对于任何数据库来说都是最简单的方法:
tag (id, name,parent_id),其中 parent_id 指的是父标签的 id

I think this is the simplest way for any database:
tag (id, name, parent_id), where parent_id refers to id of the parent tag.

世态炎凉 2024-11-22 04:02:49

您正在使用两个数据源,但是,您似乎正在混合两者。

一种数据是您的列表条目,它似乎是线性的、非分层的。

例如,电影列表。

另一个数据源是分层数据的集合(“标签目录”)。

例如电影风格列表。

+---Styles
  +---Comedy
    +---KidsComedy
    +---SomeComedy
    +---LOLComedy
  +---Action
    +---SomeAction
    +---GrabYourCouchSofaAction
  +---Drama
    +---SomeDrama
    +---LotsOfTearsDrama
    +---EvenToughGuysWillCryDrama
  +---Horror
    +---SoftHorror
    +---HardHorror
    +---Gore
  +---SciFi

每部电影都可以与多种电影风格相关联:

  • "StarWars:The Phantom Menace": {"SciFi,"SomeDrama","SoftHorror","SomeAction"}
  • "StarTrek:First Contact": {"SciFi,"SomeDrama"," SomeComedy"}

在数据库设计方面,您应该至少有 3 个表或实体对象:

  • List Entries = {ListEntryID, ListEntryTitle, ...}
  • 电影类型标签/样式 = {TagID、TagTitle、...}
  • 电影样式 = {TagForListEntryID、ListEntryID、TagID、...}

祝你好运。

You are using 2 sources of data, but, seems you are mixing both.

One data is your list entries, that seems to be lineal, non hierarchical.

For example, a list of movies.

The other source of data, its a collection of hierarchical data ("tags catalog").

For example a list of movie styles.

+---Styles
  +---Comedy
    +---KidsComedy
    +---SomeComedy
    +---LOLComedy
  +---Action
    +---SomeAction
    +---GrabYourCouchSofaAction
  +---Drama
    +---SomeDrama
    +---LotsOfTearsDrama
    +---EvenToughGuysWillCryDrama
  +---Horror
    +---SoftHorror
    +---HardHorror
    +---Gore
  +---SciFi

Each movie can be associated with several movie styles:

  • "StarWars:The Phantom Menace": {"SciFi,"SomeDrama","SoftHorror","SomeAction"}
  • "StarTrek:First Contact": {"SciFi,"SomeDrama","SomeComedy"}

In terms of Database design, you should have unleast 3 tables or Entity Objects:

  • List Entries = {ListEntryID, ListEntryTitle, ...}
  • Movie Genres Tags / Styles = {TagID, TagTitle, ...}
  • Styles For Movie = {TagForListEntryID, ListEntryID, TagID, ...}

Good Luck.

鹤舞 2024-11-22 04:02:49

使用 XML 格式,这将帮助您将节点存储为父节点和子节点
它可以有n个节点并且易于形成和处理。
注意:下面只是一个例子,所以你可以通过这种方式处理数据。

<Menu>
  <Menuitem1>
      <submenu1>
         <submenu1>
            <submenu1.1/> 
          </submenu1>        
      </submenu1>
  </Menuitem1>

  <Menuitem1>
      <submenu1>
      </submenu1>
  </Menuitem1>
</Menu>

我想这可能对你有帮助。

Use the XML format which will help you in storing the nodes as parent and child
It can have n number of nodes and easy to form and handle.
Note: The below one is just an example, So by this way you can handle the data.

<Menu>
  <Menuitem1>
      <submenu1>
         <submenu1>
            <submenu1.1/> 
          </submenu1>        
      </submenu1>
  </Menuitem1>

  <Menuitem1>
      <submenu1>
      </submenu1>
  </Menuitem1>
</Menu>

I think this may help you.

紫竹語嫣☆ 2024-11-22 04:02:49

这就是我解决这个问题的方法:首先,我将绘制一个领域模型。在你的情况下,它看起来像:

List(1)----contains----(0..*)-->ListItem
ListItem(0..1)----hasTags--(0..*)-->Tag
Tag(0..1)-----hasSubTags---(0..*)-->Tag

这使得问题变得明确,没有任何疑问的余地。

现在,将其转换为数据模型。这非常简单:为每个关系引入合适的 PrimaryKey-ForeignKey 映射。多对多关系应该使用中间的新表分成两个 1-M 关系。

您在此阶段拥有的数据模型在功能上应该是正确的,但可能存在性能问题。现在是您关注所需查询并相应优化表结构的时候了。

(从领域模型开始的另一个类似的细化之旅也将为您提供最终类模型的设计)

希望这种方法有所帮助。

This is how I would approach the problem:First, I will draw a domain model. In your case it looks like:

List(1)----contains----(0..*)-->ListItem
ListItem(0..1)----hasTags--(0..*)-->Tag
Tag(0..1)-----hasSubTags---(0..*)-->Tag

This makes the problem explicit leaving no room for doubts.

Now, translate this to a data-model. This is pretty straightforward: for each relation introduce suitable PrimaryKey-ForeignKey mappings.Many-to-Many relations should be split into two 1-M relations using a new table in between.

The data-model you have at this stage should be functionally correct, but could have performance issues. Now is the time for you to focus on the queries that you would want and optimize the table structure accordingly.

(Another similar refinement trip starting from the domain model will give you the design for the final class model as well)

Hope this approach helps.

海未深 2024-11-22 04:02:49

请参阅我的回答。我存储所有级别的父级 - 树的构建和查询所有后代非常容易。

See my answer here. I store parents for all the levels - the tree building and querying all descendants is extremely easy.

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