简体   繁体   English

如果我只有几对多,我还应该使用多对多数据库结构吗?

[英]Should I still use many-to-many db structure if I only have a few-to-many?

I'm new to web development and could really use some advice.我是 web 开发的新手,真的可以使用一些建议。 Thank you all in advance!谢谢大家!

I have a form where I ask users to rate their experience in several areas (Mental, Physical, Fun) from 1 - 10. There are about 10 areas in all.我有一个表格,要求用户从 1 到 10 对他们在几个领域(心理、身体、乐趣)的体验进行评分。总共大约有 10 个领域。 Is it worth it to make a table of areas and a intersection table with areaID and userID?用 areaID 和 userID 制作区域表和交集表是否值得? In this case, wouldn't it be easier to just create one table with 11 columns (one for each area plus a userID)?在这种情况下,只创建一个包含 11 列的表(每个区域一个加上一个用户 ID)不是更容易吗?

I understand that a table of 'areas' with a key of areaID would be the most robust, and allow me to later add more areas but I really don't see myself doing that.我知道带有 areaID 键的“区域”表将是最强大的,并允许我稍后添加更多区域,但我真的不认为自己会这样做。 Also, as I only expect <1000 users, would adding another column to a MySQL db be a lot of work?另外,因为我只希望 <1000 个用户,将另一列添加到 MySQL db 会做很多工作吗?

Finally, what is the cutoff point where I actually should use a many-to-many structure?最后,我实际上应该使用多对多结构的截止点是什么? Later in the form I have users rate ~30 fields from 1 - 4. Should I use the two table system there?稍后在表格中,我让用户从 1 到 4 对约 30 个字段进行评分。我应该在那里使用两个表系统吗?

Thanks again!再次感谢!

It'd be better to use the separate child table.最好使用单独的子表。 The SQL overhead for a join is minimal, and you get total flexibility in how many/few entries you want to allow, without ever having to change the database structure.连接的 SQL 开销最小,并且您可以完全灵活地选择要允许的条目数,而无需更改数据库结构。

It may only be 10 areas now, but PHBs are notorious for changing their minds about something that was "bedrock" on the hour before launch.现在可能只有 10 个区域,但 PHB 因在发射前一小时改变对“基石”的想法而臭名昭著。

I would always model a many-to-many relationship with a proper intersection table because you can never predict what the future will bring.总是会 model 与适当的交集表建立多对多关系,因为您永远无法预测未来会带来什么。

Suppose you want to count the number of areas per user.假设您要计算每个用户的区域数。 How would you do that if you use a single table with 11 columns?如果你使用一个有 11 列的表,你会怎么做?

Implement this with the intersection table.用交集表实现这一点。 You will be glad you did.你会很高兴你做到了。

Both makes sense, but I would choose the many-to-many approach as you can then specify in the areas table the name of each area (and maybe add some comments what 1 or 10 for that specific area means, etc..)两者都是有道理的,但我会选择多对多方法,因为您可以在区域表中指定每个区域的名称(并可能添加一些注释,该特定区域的 1 或 10 意味着什么,等等。)

There's no wrong answer to this, but consider this: are you sure that that number will remain fixed at ten?对此没有错误的答案,但请考虑一下:您确定该数字将保持在 10 不变吗?

Personally, I would still use the many-to-many relationship.就个人而言,我仍然会使用多对多关系。 It's the more elegant solution, and, in my mind, better.这是更优雅的解决方案,而且在我看来,更好。 However, doing a job well is often a waste of time.然而,把工作做好往往是在浪费时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM