灵活类别/子类别系统中条目的最佳数据库结构?

时间:2012-11-26 00:53:58

标签: php mysql database-design

我希望将评论存储在灵活的类别和子类别系统中,目前我正在为此设计数据库结构。我知道如何做到这一点,但我不完全确定它是否能够更优雅和/或更高效。这些是我的想法 - 如果有人可以评论是否/如何改善这一点我会非常感激。

(为了使这篇文章简洁,我只列出表格的重要字段)

1。)评论存储在“评论”表中。它包含以下字段:

id: uniquite ID, auto-incrementing.
title: the title that will show up in <head><title>, etc.
stub: a version of the title without spaces, special chars, etc. so it can be part of the URL/URI
text: the actual content

2.。)所有类别都在同一个表中“类别”

id: unique ID, auto-incrementing.
title: the full title/name of the categorie how it will be output on the website
stub: version of the title that will be shown in the URL/URI.
parent_id: if this is a subcategory, here is the categories.id of the parent category. Else this is 0.
order_number: simple number to order the categories by (for display in the navigation menu)

3。)现在我需要一个指标,评论哪些类别。可以是多个。我的第一个想法是在类别中添加“review_list”字段,并将其包含所有评论。应该属于此类别。但是我认为在类别中添加和删除评论会很麻烦并且“不优雅”。所以我目前的想法是有一个表“review_in_category”,并为每个评论类别关系都有一个条目。结构是:

id: Unique ID, auto-increment. 
review_id: the reviews.id 
category_id: the categories.id

因此,如果评论分为3个不同的类别,则会在“review_in_category”表中生成3个条目。

这个想法是,当用户打开www.mydomain.de/animation/sci-fi/时,包装器脚本会将URL分解为其部分。如果它找到了包含category.stub =“sci-fi”的多个类别,它将检查哪些类别具有存根“动画”的父类别。一旦识别出正确的类别(大多数时候存根都是唯一的,所以可以跳过这个检查)我想从“review_in_category”中选择所有review_id,其中category_id与包装器脚本确定的匹配。所有review_id都放在一个数组中。循环将遍历此数组并编写SELECT语句以列出所有审阅标题(并使用存根值创建指向它们的链接)“SELECT title,stub FROM reviews WHERE id = review_list [$ counter]”然后添加“OR id = review_list [$ counter]“直到数组完全移动。

所以我的问题是: - 该方法是我创建单个SELECT语句,可能有大量“OR id =”部分是“elegent”和/或有效的方法来处理这种情况还是有更好的变体? - 使用“分类法”式表(review_in_category)是否有意义,或者将“成员资格”/“关系”直接存储在评论或类别表中会更好吗? - 任何其他想法......我刚开始学习这些东西,并感谢任何反馈。

谢谢

1 个答案:

答案 0 :(得分:0)

您的设计看起来很合理。

要检索某个类别中的所有评论,您应该使用加入:

SELECT reviews.title, reviews.stub FROM reviews, review_in_category WHERE reviews.id = review_in_category.review_id AND category_id = $category