设计具有层次结构/子类别的 SQL 表
我有一个看起来像这样的表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不直接将 ParentID 列和 FK 添加到 PK 中呢?
Why not just add a ParentID column and FK to the PK?
我会在两个表中执行此操作,每个外键都来自“类别”表:
“类别”表中的数据将如下所示:
“关键字”表中的数据将如下所示:
I'd do it in two tables with each foreign key coming from the Categories table:
The data in the Categories table would look like:
and that data in the Keywords table would look like:
您只需要一张表来表示 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.
我会使用这样的两张桌子。
I'd use a two tables like this.
如果您希望稍后重命名或重新排列类别,则将其拆分可能是有意义的:
权衡两者的利弊然后做出决定。
It might make sense to split it up if you expect to rename or rearrange your categories later:
Weigh the cons and pros of both and then make a decision.m