设计具有层次结构/子类别的 SQL 表

发布于 2024-08-19 13:44:15 字数 155 浏览 6 评论 0原文

我有一个看起来像这样的表:

ID |关键词|类别 |子类别 |子子类别| Sub-Sub-Sub-Category

我需要将其分成两个表(一个关键字表和一个带有父 ID 的类别表) 如果一个关键字只能属于一个类别、子类别...等。意味着没有重复。还有必要拆分吗?

I have a table that looks something like this:

ID | Keyword | Category | Sub-Category | Sub-Sub-Category | Sub-Sub-Sub-Category

Do i need to split it up in two tables (a keyword table and a categories table with parent id)
if one keyword can only belong to one category,sub-category...etc. meaning there are no repetition. is there still a need to split it up?

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

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

发布评论

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

评论(5

半暖夏伤 2024-08-26 13:44:16

为什么不直接将 ParentID 列和 FK 添加到 PK 中呢?

Why not just add a ParentID column and FK to the PK?

铁轨上的流浪者 2024-08-26 13:44:15

我会在两个表中执行此操作,每个外键都来自“类别”表:

Keywords 
id (PK)
keyword
category_id (FK)

Categories
category_id (PK)
category
parent_category_id (FK)

“类别”表中的数据将如下所示:

category_id    category    parent_category_id
1              Food        null
2              meat        1
3              organic     1
4              fruit       3

“关键字”表中的数据将如下所示:

id     keyword    category_id
1      grapes     4
2      chicken    2

I'd do it in two tables with each foreign key coming from the Categories table:

Keywords 
id (PK)
keyword
category_id (FK)

Categories
category_id (PK)
category
parent_category_id (FK)

The data in the Categories table would look like:

category_id    category    parent_category_id
1              Food        null
2              meat        1
3              organic     1
4              fruit       3

and that data in the Keywords table would look like:

id     keyword    category_id
1      grapes     4
2      chicken    2
好倦 2024-08-26 13:44:15

您只需要一张表来表示 1-1 映射。要表示一对多或多对多映射,您应该使用多个表。

如果一个关键词只能对应一个类别/子类别/子子类别,那么你当前的布局应该没问题。

需要注意的是:如果您想根据关键字进行搜索,则分隔表可能会提高性能。执行整数搜索要快得多。

将关键字值存储在另一个表中的讨论大致对应于 在另一个表中存储国家/地区名称(大部分是静态的)的讨论。使用另一个表的一些关键优势可能是(口语)语言独立性、快速搜索以及以后易于更新等。

You only need one table to represent a 1-1 mapping. To represent 1-many or many-many mappings, you should use multiple tables.

If a keyword can only correspond to one category/sub-category/sub-sub-category, your current layout should be fine.

One caveat: if you want to search based on the keyword, there could be performance gains for separating the tables. It's much faster to perform an integer search.

The discussion of storing the keyword values in another table coarsely corresponds to this discussion of storing country names (which are mostly static) in another table. Some key advantages of using another table might be things like (spoken) language independence, fast searching, and ease of updating later on.

汐鸠 2024-08-26 13:44:15

我会使用这样的两张桌子。

   Categories
-------------------
PK,FK1 | CategoryID
       | Keyword 
       | Category 

  SubCategories
--------------------
PK,FK1 | CategoryID
PK,FK1 | SubCategoryID

I'd use a two tables like this.

   Categories
-------------------
PK,FK1 | CategoryID
       | Keyword 
       | Category 

  SubCategories
--------------------
PK,FK1 | CategoryID
PK,FK1 | SubCategoryID
夏末染殇 2024-08-26 13:44:15

如果您希望稍后重命名或重新排列类别,则将其拆分可能是有意义的:

  • 如果您保持原样,则必须执行重命名/重新组织步骤(更改类别/子类别) -category/sub-sub-category/sub-sub-sub-category fields) 此表中包含该 (((sub)sub)sub) 类别的每一行。这会导致更复杂的查询,并且如果此关键字表中有很多行,则可能会出现性能问题(=数据库需要一段时间才能完成);另一方面,查询(读取)将尽可能快。
  • 如果将其拆分,则更新 (((sub)sub)sub)category 将仅对较少的行进行,但查询(读取)将花费更长的时间,因为它必须使用两个(或更多)表。

权衡两者的利弊然后做出决定。

It might make sense to split it up if you expect to rename or rearrange your categories later:

  • if you leave it as it is, you will have to do that rename/reorganize step (changing the category/sub-category/sub-sub-category/sub-sub-sub-category fields) for every row in this table that contains that (((sub)sub)sub)category. that results in a more complex query, and if you have very many rows in this keyword table, it might be a performance issue (=take a while for the database to do); on the other hand, queries (reading) will be as fast as possible.
  • if you split it up, then updating (((sub)sub)sub)category will only to be done to less rows, but query (read) will take longer because it has to work with two (or more) tables.

Weigh the cons and pros of both and then make a decision.m

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