在数据库中存储数据结构的最佳方式是什么?

发布于 2024-10-20 19:42:10 字数 2571 浏览 1 评论 0原文

我正在建立一家网上商店,但遇到了问题。 我将拥有直接价格的产品(例如 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:
database diagram

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香草可樂 2024-10-27 19:42:10

您可以轻松地将价格存储在产品旁边,并使用这样的查询来获取产品。

SELECT p.productid, 
       p.productname, 
       CASE 
         WHEN cb.combinationid > 0 THEN cb.price 
         ELSE p.price
       END, 
       cb.combinationid 
FROM   products p 
       LEFT JOIN specifications sp 
         ON sp.productid = p.productid 
       LEFT JOIN specvalues spv 
         ON spv.specid = sp.specid 
       LEFT JOIN combinationparts cbp 
         ON cbp.specvalueid = spv.specvalueid 
       LEFT JOIN combinations cb 
         ON cb.combinationid = cbp.combinationid 
WHERE  p.productid IN ( 1, 2 ) 
       AND CASE 
             WHEN cb.combinationid > 0 THEN cb.combinationid IN ( -100, 1, 2 ) 
             ELSE 1 = 1 
           END 

这需要输入产品编号和组合。由于组合可能会丢失,因此我添加了 -100 作为默认值

You could easily store the price next to the product and use a query like this to get the products.

SELECT p.productid, 
       p.productname, 
       CASE 
         WHEN cb.combinationid > 0 THEN cb.price 
         ELSE p.price
       END, 
       cb.combinationid 
FROM   products p 
       LEFT JOIN specifications sp 
         ON sp.productid = p.productid 
       LEFT JOIN specvalues spv 
         ON spv.specid = sp.specid 
       LEFT JOIN combinationparts cbp 
         ON cbp.specvalueid = spv.specvalueid 
       LEFT JOIN combinations cb 
         ON cb.combinationid = cbp.combinationid 
WHERE  p.productid IN ( 1, 2 ) 
       AND CASE 
             WHEN cb.combinationid > 0 THEN cb.combinationid IN ( -100, 1, 2 ) 
             ELSE 1 = 1 
           END 

this needs as input product numbers and combinations. Since the combinations can be missing, I've added -100 as default value

ˉ厌 2024-10-27 19:42:10

为什么不在产品表中添加一个可为空的价格列?

如果您想通过单个查询轻松访问所有价格,您可以使用视图:

create view combPrices as
select ProductID, null as Variety, Price from tbProducts where Price is not null
union
select tbProductDesc.ProductID, tbProductDesc.Variety, tbProductDesc.Price 
from tbProducts
inner join tbProductDesc on tbProducts.ProductID = tbProductDesc.ProductID

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 :

create view combPrices as
select ProductID, null as Variety, Price from tbProducts where Price is not null
union
select tbProductDesc.ProductID, tbProductDesc.Variety, tbProductDesc.Price 
from tbProducts
inner join tbProductDesc on tbProducts.ProductID = tbProductDesc.ProductID
流心雨 2024-10-27 19:42:10

嗯,看来您需要两个具有一对多关系的表。
像这样的东西:(伪代码)

Products
   ProductID int autoincrement
   ProductName text

ProductDesc
   ProductDescID int autoincrement
   ProductID int foreign_key
   ProductDescription text
   Price float

所以你可以这样填写:

Products
   1 'HTC Touch 2'
   2 'White Shirt'
   3 'Blue Shirt'
   4 'Nike Exclusive T-Shirt 2011'

ProductDesc
   1 1 'Smartphone' 299.00
   2 2 'Large' 10.00
   3 2 'XL' 15.00
   4 3 'Large' 11.00
   5 3 'XL' 16.00
   6 4 'L White' 10.00 
   7 4 'XL White' 15.00
   8 4 'L blue' 11.00
   9 4 'XL blue' 16.00

那么一种产品有一个价格还是有多个价格并不重要。

Hmm, it seems like you're going to need two tables with a one to many relationship.
Something like: (Psudocode)

Products
   ProductID int autoincrement
   ProductName text

ProductDesc
   ProductDescID int autoincrement
   ProductID int foreign_key
   ProductDescription text
   Price float

So you would then fill it like:

Products
   1 'HTC Touch 2'
   2 'White Shirt'
   3 'Blue Shirt'
   4 'Nike Exclusive T-Shirt 2011'

ProductDesc
   1 1 'Smartphone' 299.00
   2 2 'Large' 10.00
   3 2 'XL' 15.00
   4 3 'Large' 11.00
   5 3 'XL' 16.00
   6 4 'L White' 10.00 
   7 4 'XL White' 15.00
   8 4 'L blue' 11.00
   9 4 'XL blue' 16.00

Then it wouldn't matter if a product had one price or many prices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文