在数据库中存储数据结构的最佳方式是什么?
我正在建立一家网上商店,但遇到了问题。 我将拥有直接价格的产品(例如 HTC Touch 2 智能手机:299.00 美元),但同时我将拥有基于品种组合价格的产品: 例如:
product: Nike Exclusive T-Shirt 2011
varieties:
Sizes: L, XL
Colors: red, blue
combinations will be:
L white - $10
XL white - $15
L blue - $11
XL blue - $16
我需要数据库结构的最佳方式(或唯一有效的:)),我可以在其中存储两种类型的产品,并且如果我想从网页上包含两种类型产品的类别中列出我的产品(单一价格,多个价格)我可以构建一个mysql查询来获取同一查询中的所有产品。
谢谢
更新
我肯定会有以下表格:[products]
、[varieties]
(颜色、尺寸)、[varietyValues]
(其中存储产品的主要颜色和尺寸 - 每个都是表中的一行 {productId +varietyId + value (red, S, M, green, XL, etc...)} )。在此之后,我将有另一个表 [combinations]
,在 [combinations]
和 [ 之间具有多对多 [n-to-m] 关系varieValues]
这将生成一个新表[combPrices]
。这个新的 n 到 m 表的每一行都有一个价格。
现在的问题是我不知道如何在这个数据结构中存储单价产品。
更新 2
在这张图片中,您可以看到数据库图表,我认为这对于多价产品来说是可以的:
主要问题: 由于这是一个网上商店,人们会将商品放入购物车。我认为插入购物车的商品应该来自同一个表(在我们的例子中,它将是 [combinations]
表,因为存储了价格)。
为了更清楚起见,以下是这些表的一些数据:
[产品]
productid | productName
1 | Nike T-Shirt
2 | HTC Touch 2 Smartphone
[规格]
specId | productId | specName
1 | 1 | Size
2 | 1 | Color
[规格值]
specValueId | specId | svValue
1 | 1 | L
2 | 1 | XL
3 | 2 | white
4 | 2 | blue
5 | 2 | red
[组合]
(商品放入购物车)
combinationId | price | description
1 | 10 | White L Nike T-Shirt
2 | 15 | White XL Nike T-Shirt
3 | 11 | Blue L Nike T-Shirt
4 | 16 | Blue XL Nike T-Shirt
5 | 18 | Red XL Nike T-Shirt
[combinationParts]
nmid | combinationId | specValueId
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 2 | 3
5 | 3 | 1
1 | 3 | 4
2 | 4 | 2
3 | 4 | 4
4 | 5 | 2
5 | 5 | 5
我希望我的图表和数据库人口确实有意义:)。
因此,最后一个问题是如何存储单价产品(HTC Touch 2 智能手机),以便可以像多价产品一样将其添加到购物车。
I am building an online shop and I have a problem.
I will have products which have a direct price (for example HTC Touch 2 Smartphone: $299.00 ), but in the same time I will have products which have prices for combinations based on varieties:
for example:
product: Nike Exclusive T-Shirt 2011
varieties:
Sizes: L, XL
Colors: red, blue
combinations will be:
L white - $10
XL white - $15
L blue - $11
XL blue - $16
I need the best way (or the only one working :) ) of database structure, where I can store both types of products, and if I want to list my products from a category which contains both types of products on the webpage (single price, multiple prices) i can build a mysql query to get all the products in the same query.
thanks
UPDATE
I will have for sure the following tables: [products]
, [varieties]
(color, size), [varietyValues]
(which stores what king od colors and what kind of sizes does the product have - each one is a row in the table {productId + varietyId + value (red, S, M, green, XL, etc...)} ). After this one I would have another table [combinations]
, with a many-to-many [n-to-m] relationship between [combinations]
and [varietyValues]
which will result a new table [combPrices]
. Each row of this new n-to-m table will have a price.
Now the problem is I can't figure out how to store single-price products in this data structure.
UPDATE 2
In this image you can see the database diagram, which I think would be ok for the multiple-price products:
THE MAIN PROBLEM:
Since this is a webshop, people will put items in the shopping cart. I think the items inserted into the shopping cart should be from the same table (in our case it would be the [combinations]
table, since there are the prices stored).
Here are some data for these tables, just to be more clear:
[products]
productid | productName
1 | Nike T-Shirt
2 | HTC Touch 2 Smartphone
[specifications]
specId | productId | specName
1 | 1 | Size
2 | 1 | Color
[specvalues]
specValueId | specId | svValue
1 | 1 | L
2 | 1 | XL
3 | 2 | white
4 | 2 | blue
5 | 2 | red
[combinations]
(items into the cart)
combinationId | price | description
1 | 10 | White L Nike T-Shirt
2 | 15 | White XL Nike T-Shirt
3 | 11 | Blue L Nike T-Shirt
4 | 16 | Blue XL Nike T-Shirt
5 | 18 | Red XL Nike T-Shirt
[combinationParts]
nmid | combinationId | specValueId
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 2 | 3
5 | 3 | 1
1 | 3 | 4
2 | 4 | 2
3 | 4 | 4
4 | 5 | 2
5 | 5 | 5
I hope my diagram and database population does make sense :) .
So the final question is how can I store the single price products (HTC Touch 2 Smartphone) so it can be added to shopping cart just like the multiple price products.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以轻松地将价格存储在产品旁边,并使用这样的查询来获取产品。
这需要输入产品编号和组合。由于组合可能会丢失,因此我添加了 -100 作为默认值
You could easily store the price next to the product and use a query like this to get the products.
this needs as input product numbers and combinations. Since the combinations can be missing, I've added -100 as default value
为什么不在产品表中添加一个可为空的价格列?
如果您想通过单个查询轻松访问所有价格,您可以使用视图:
Why don't you just add a nullable price column to your products table ?
If you want to easily access all the prices with a single query, you could use a view :
嗯,看来您需要两个具有一对多关系的表。
像这样的东西:(伪代码)
所以你可以这样填写:
那么一种产品有一个价格还是有多个价格并不重要。
Hmm, it seems like you're going to need two tables with a one to many relationship.
Something like: (Psudocode)
So you would then fill it like:
Then it wouldn't matter if a product had one price or many prices.