简体   繁体   English

如何在mysql数据库中创建多个相关表

[英]How to create multiple related tables in mysql database

My title might not really convey what i'm trying to achieve, I have some tables which are related but i can't figure out how to create this relationship to make storing and retrieving data much easy. 我的标题可能没有真正传达我想要达到的目的,我有一些相关的表,但是我不知道如何创建这种关系以使存储和检索数据变得更加容易。 Below is what i was able to come up with. 以下是我能想到的。

color table
colorId(PK)
productId(FK)
colorName 

size table
sizeId(PK)
productId(FK)
size


product table
productId(PK)
priceId(FK)
Qty
Name
Title

price table
priceId(PK)
productId(FK)

Now my problem is i have product with different variety for example Men's blue addidas glide running shoes with productId 1, this product have different sizes and different color and the prices varies on sizes and color. 现在我的问题是我有不同品种的产品,例如,带有productId 1的Men's blue addidas glide running shoes ,该产品具有不同的尺寸和颜色,价格随尺寸和颜色而变化。 example, if this pair of shoe color BLUE size 11 sells for $50, the same pair of shoe with bigger size and different color say size 12 color red might sell for $55, also this same pair of shoe color blue might have only size 11 available in stock and color blue size 12 of this pair might not be available, how do i create my table to save all the differences between color and sizes, and also prices. 例如,如果这双颜色为BLUE 11的鞋子的售价为50美元,那么同一双尺寸更大且颜色不同的鞋子的尺寸为12,红色的鞋子可能售价为55,那么这双颜色为蓝色的鞋可能只有11的尺寸库存和颜色蓝色这双12号的大小可能无法使用,我如何创建表以保存颜色和尺寸之间的所有差异以及价格。 Any help on this, thanks 任何帮助,谢谢

You probably shouldnt have every single attribute in it's own table, a potentially better way would be to have a product table and a variant table. 您可能不应该在自己的表中拥有每个属性,一种可能更好的方法是拥有一个product表和一个variant表。 This way you could have price, size, color, quantity available, etc all in the same table, then join on the product ie Men's blue adidas glide running shoes . 这样,您可以在同一张桌子上获得价格,尺寸,颜色,数量等信息,然后加入产品,即Men's blue adidas glide running shoes Your variant table would look something like this: 您的变量表如下所示:

| variant_id | product_id | price | colour_id | size_id | quantity_available | in_stock |
|          1 |          1 | 50.00 |         1 |     11  |                 20 |        1 |
|          2 |          1 | 55.00 |         2 |     12  |                  0 |        1 |

Then you could load each variant individually, and have the quantity_available updated whenever you make a sale. 然后,您可以分别加载每个变体,并在每次销售时更新quantity_available I've also included an in_stock boolean so you can override whether stock appears on your site without having to adjust the quantity_available . 我还包括一个in_stock布尔值,因此您可以覆盖库存是否显示在您的网站上,而无需调整quantity_available You'd then have the variant_id on the purchase table so you can join these later. 然后,您会在purchase表上显示variant_id ,以便稍后加入。 Also, I like having sizes and colours on their own tables so you can put a taxonomy in place to aggregate all they blues say, or all the XS, S, M, L, XL. 另外,我喜欢在自己的桌子上放大小和颜色,这样您就可以进行分类,以汇总他们喜欢蓝色或所有XS,S,M,L,XL的信息。

Something like: 就像是:

| colour_id | name          | base_colour_id |
|         1 | Royal Blue    |              3 |
|         2 | Spanish Blue  |              3 |
|         3 | Blue          |              3 |
|         4 | Ultramarine   |              3 |
|         5 | Green         |              5 |
|         6 | Mint Green    |              5 |

This way you can add more unique colour variants and still report on base colours or full colour names. 这样,您可以添加更多独特的颜色变体,并且仍然可以报告基色或全色名称。 Here you'd use: SELECT * FROM variant_colour a JOIN variant_colour b ON b.colour_id = a.base_colour_id you'd get: 在这里,您将使用: SELECT * FROM variant_colour a JOIN variant_colour b ON b.colour_id = a.base_colour_id您将获得:

| colour_id | name          | base_colour_id | colour_id | name          | base_colour_id |
|         1 | Royal Blue    |              3 |         3 | Blue          |              3 |
|         2 | Spanish Blue  |              3 |         3 | Blue          |              3 |
|         3 | Blue          |              3 |         3 | Blue          |              3 |
|         4 | Ultramarine   |              3 |         3 | Blue          |              3 |
|         5 | Green         |              5 |         5 | Green         |              5 |
|         6 | Mint Green    |              5 |         5 | Green         |              5 |

This same idea can be used for sizes etc so you don't end up with hundreds of unique attribute variants that make your info impossible to report on. 可以将相同的想法用于大小等,因此最终不会出现数百个独特的属性变体,这些变体使您的信息无法报告。

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

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