如何创建更好的表结构
对于我的网站,我有表格
Category :: id | name
Product :: id | name | categoryid
现在每个类别可能有不同的大小,为此我还创建了一个表格
Size :: id | name | categoryid | price
现在的问题是每个类别也有不同的成分,客户可以选择添加到他购买的内容中 产品。而且这些原料的大小不同,价格也不同。 为此,我还有一个表,就像
Ingredient :: id | name | sizeid | categoryid | price
我不确定这个结构是否真的标准化一样。有人可以帮助我优化这个结构以及这个结构需要哪些索引吗?
For my website i have tables
Category :: id | name
Product :: id | name | categoryid
Now each category may have different sizes, for that I have also created a table
Size :: id | name | categoryid | price
Now the problem is that each category has also different ingredients that customer can choose to add to his purchased
product. And these ingredients have different prices for different sizes.
For that I also have a table like
Ingredient :: id | name | sizeid | categoryid | price
I am not sure if this Structure really normalized is. Can someone please help me to optimize this structure and which indexed do i need for this Structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果某种成分的价格仅取决于其大小而不取决于类别,那么请尝试以下操作:
If an ingredient's price depends only on its size and not also the category then try this:
类别::id |姓名
产品:: id |名称 |类别ID
尺寸:: ID |姓名 |
成分:: id |姓名 |
类别_大小:: id |类别ID |尺寸ID |价格
类别_成分:: id |类别ID |成分ID |价格
所有复杂的实体都被分解为尽可能少的部分(单个表)。然后利用外键关系将它们进行逻辑关联。
Category :: id | name
Product :: id | name | categoryid
Size :: id | name |
Ingredient :: id | name |
Category_Size :: id | categoryid | SizeID | Price
Category_Ingredient :: id | categoryID | IngredientID | Price
All complex entities are broken into least possible parts (individual table). Then used foreign key relations to relate them logically.
结构对我来说看起来不错。当然,PK 将通过将它们定义为 PK 来进行索引。
您需要为所有 FK 添加索引:
根据表的大小以及您可能希望按字母顺序显示项目的事实,您可能还希望对所有名称字段建立索引。
Structure look good to me. PK will be indexed by defining them as PK of course.
You need to add indexes for all FKs:
Depending on the size of your tables, and on the fact that you might want to display items alphabetically, you might also want to index all the name fields.