douxieshang5577 2011-12-30 01:02
浏览 73
已采纳

数据库建模:针对多个元素的多个类别的最佳方法

Let's say I have 10 books, each book has assigned some categories (ex. :php, programming, cooking, cookies etc).

After storing this data in a DB I want to search the books that match some categories, and also output the matched categories for each pair of books.

What would be the best approach for a fast and easy to code search:

1) Make a column with all categories for each book, the book rows would be unique (categs separated by comma in each row ) -> denormalisation from 1NF

2) Make a column with only 1 category in each row and multiple rows per book

I think it is easier for other queries if I store the categories 1 by 1 (method 2), but harder for that specific type of search. Is this correct?

I am using PHP and MySQL.

PPS : I know multi relational design, I prefer not joining every time the tables. I'm using different connection for some tables but that's not the problem. I'm asking what's the best approach for a db design for this type of search: a user type cooking, cookies, potatoes and I want to output pairs of books that have 1,2 more or all matched categs. I'm looking for a fast query, or php matching technique for this thing... Tell me your pint of view. Hope I'm understood

  • 写回答

4条回答 默认 最新

  • dqhmtpuy94946 2011-12-30 01:11
    关注

    Use method 2 -- multiple rows per book, storing one category per row. It's the only way to make searching for a given category easy.

    This design avoids repeating groups within a column, so it's good for First Normal Form.

    But it's not just an academic exercise, it's a practical design that is good for all sorts of things. See my answer to Is storing a comma separated list in a database column really that bad?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog