Trouble understanding extra table reasoning in setting up SQL tables, can someone try to explain clearly?

huangapple go评论60阅读模式
英文:

Trouble understanding extra table reasoning in setting up SQL tables, can someone try to explain clearly?

问题

我需要设置两个下拉选择框,并且我对我已有的解决方案理解困难。

我的情况
我有一个产品选择,然后会基于所选产品填充颜色选择。多个产品可能会有相同的颜色。

由于似乎 SQL 数据库不能轻松地包含列中的数组值(或者可能可以,但我还没有找到返回正确结果的好解决方案),我搜索了一些替代解决方案。我找到的一个建议创建三个表,一个 PRODUCT_ID 表,一个 COLOR_ID 表,以及一个 PRODUCT_COLOR 表。

我不明白为什么这样做会更容易或更少工作,无论是在添加更多产品还是颜色时。下面是一个非常接近我所需的解决方案,但为什么要这样做,而不是在两个表中设置,然后使用类似 SELECT color FROM COLORS where PRODUCT_ID = ID 这样的查询,其中 ID 值是产品选择中的选定值?我已经添加了其他产品和颜色,以说明它看起来是正确的,基于我添加更多的产品/颜色信息。这个解决方案似乎只是在颜色上添加了一个第三个表,然后在 PRODUCT/COLOR 表中会有与我的第一个想法中的 COLORS 表相同的设置?

是否有一种我不知道的方法,可以在单个列中使用数组,以便您只需列出颜色的一个实例,然后与该颜色关联的所有产品 ID?我知道这是与我的第一个问题不同的问题,但我只是好奇是否有可能减少行和列的数量,以便我不必为 100 多个产品和每个产品的多个颜色手动设置大量行或感觉没有必要的额外表。第三个表是安全问题还是只是为了添加可用性?我不需要可用性,但我也看到了一个类似的解决方案,它交叉引用了疾病和症状,使用了相同的三个表的想法,一个 DISEASES,一个 SYMPTOMS,以及一个 DISEASES_V_SYMPTOMS 使用 ids

--我的想法--

产品

产品 ID
T恤 1
卫衣 2

颜色

颜色 产品ID
红色 1
红色 2
绿色 1
绿色 2
蓝色 1

--解决方案代码问题--
以下是否会产生与这种设置类似的东西?

产品

产品 ID
T恤 1
卫衣 2

颜色

颜色 ID
红色 1
绿色 2
蓝色 3

产品/颜色

产品ID 颜色ID
1 1
1 2
1 3
2 1
2 2
创建表 [产品]
(
ID INT IDENTITY(1,1) 主键
,名称 NVARCHAR(250)
)

创建表 [颜色]
(
ID INT IDENTITY(1,1) 主键
,颜色 NVARCHAR(250)
)

创建表 [产品_颜色_可用性] -- 多对多关系表
(
ID INT IDENTITY(1,1) 主键
,产品ID INT
,颜色ID INT
,可用性 bit -- 1=可用,0=不可用
)

--
-- 插入一些数据,单个产品(T恤)既是红色又是蓝色。

插入到 [产品] (名称)
VALUES ('T恤')

插入到 [颜色] (颜色)
VALUES ('红色'), ('蓝色')

插入到 [产品_颜色_可用性] ([产品ID], [颜色ID])
VALUES (1,1,1), (1,2,1)

-- 获取有关特定产品的可用性信息:

选择 P.[ID] AS '产品ID'
,P.[名称] AS '产品名称'
,C.[颜色] AS '产品颜色'
,PCA.[可用性] AS '可用性'
 [产品_颜色_可用性] PCA
左连接 [产品] P ON PCA.产品ID=P.[ID]
左连接 [颜色] C ON PCA.颜色ID=C.[ID]
WHERE P.[ID] = 1
英文:

I have the need to set up two dropdown selections, and I am having trouble with understanding the solutions I have.

My Situation
I have a product selection, that will then populate a color selection based on the product. Multiple products will have some of the same colors.

Since it seems an SQL database can't contain array values in columns easily (or maybe it can, but I haven't found a good solution that returned correct results), I searched some alternative solutions. The one I found suggested making three tables, a PRODUCT_ID table, a COLOR_ID table, and a PRODUCT_COLOR table.

I don't understand why that would be easier or less work when adding either more products or colors. Below is the solution that very closely mirrored what I need, but why would you do it that way, instead of setting it up in only two tables and then using something like SELECT color FROM COLORS where PRODUCT_ID = ID where the ID value is the selected value from the product selection, in your query? I have added additional products and colors to illustrate it in a way that seems like it's correct based on the information I have as I add more products/colors. It seems like that solution just adds a third table for the colors and then you would have the same setup in the PRODUCT/COLOR table as the COLORS table in my first idea?

Is there a way I don't know of that allows you to use arrays in a single column so you only have to list one instance of the color and then all product IDs that would be associated with the color? I know this is a different question to my first but I was just curious if it was possible to reduce the amount of lines and columns so that I wouldn't have to manually set up a ton of rows for 100+ products and multiple colors per product or additional tables that feel redundant for no reason. Is the third table a security thing or just to add in availability? I don't need availability but I also saw a similar solution that cross referenced diseases and symptoms with the same idea of three tables, one DISEASES, one SYMPTOMS, and one DISEASES_V_SYMPTOMS using ids

--MY IDEA--

PRODUCT

Product ID
T-shirt 1
Hoodie 2

COLORS

color PRODUCT_ID
Red 1
Red 2
Green 1
Green 2
Blue 1

--SOLUTION CODE PROBLEM--
Wouldn't the following produce something similar to this setup?

PRODUCT

Product ID
Tshirt 1
Hoodie 2

COLOR

COLOR ID
Red 1
Green 2
Blue 3

PRODUCT/COLOR

PRODUCT_ID COLOR_ID
1 1
1 2
1 3
2 1
2 2
CREATE TABLE [Product]
(
ID INT IDENTITY(1,1) PRIMARY KEY
,Name NVARCHAR(250)
)

CREATE TABLE [Colour]
(
ID INT IDENTITY(1,1) PRIMARY KEY
,Colour NVARCHAR(250)
)

CREATE TABLE [Product_Colour_Availability] -- Many to Many relationship table
(
ID INT IDENTITY(1,1) PRIMARY KEY
,Product_ID INT
,Colour_ID INT
,Available bit -- 1=available, 0=not available
)

--
-- Insert some data for a single product (T-Shirt) that will be both red and blue.

INSERT INTO [Product] (Name)
VALUES ('T-Shirt')

INSERT INTO [Colour] (Colour)
VALUES ('Red'), ('Blue')

INSERT INTO [Product_Colour_Availability] ([Product_ID], [Colour_ID])
VALUES (1,1,1), (1,2,1)

-- Get availability info about a specific product:

SELECT P.[ID] AS 'Product ID'
,P.[Name] AS 'Product Name'
,C.[Colour] AS 'Product Colour'
,PCA.[Available] AS 'Availability'
FROM [Product_Colour_Availability] PCA
LEFT JOIN [Product] P ON PCA.Product_ID=P.[ID]
LEFT JOIN [Colour] C ON PCA.Colour_ID=C.[ID]
WHERE P.[ID] = 1

答案1

得分: 2

不要尝试将数组存储在列值中。你可以使用逗号分隔的列表来实现,但这绝对会降低SQL查询性能的优化。将来要投入生产的人会咒骂你的名字,大声咒骂很长一段时间。(问我怎么知道这一点 Trouble understanding extra table reasoning in setting up SQL tables, can someone try to explain clearly?

你有实体:你想要在数据库中的行中建模的现实世界中的事物。这些事物之间有关系。

你的三表解决方案包括两个实体表(ProductColour)。而且,看起来它包括一个所谓的联接表,允许你构建实体之间的多对多关系。这为你的数据库变得更小且更容易搜索。

但你正确地观察到,你不会用光"small"、"red"或"t-shirt",而是用光"small red t-shirts"。所以,你最重要的实体,如果你想销售东西,就必须正确建模的是你的库存单位。SKU

因此,你的问题归结为:你希望在你的SKU实体上具有哪些属性?也许类似这样?

SKU_id  num_in_stock  Size_id  Colour_id  Product_id  description

 1234          7         1         1         1   Small red t-shirt
 1235          0         2         1         1   Medium red t-shirt
 1248          4         3         2         2   Large green hoodie

你可以使用_id值按颜色、尺寸或产品进行搜索。或者,你可以只使用文本描述并使用类似这样的SQL来搜索产品。

description LIKE '%green%'

关于这个决策取决于你想要如何组织你的产品展示和搜索功能。

英文:

Don't attempt to store arrays in column values. You can probably do it with comma-separated lists, but you definitively defeat SQL query performance optimizations by doing that. The people who will put your database into production will curse your name. Loudly. For a long time. (Ask me how I know this sometime Trouble understanding extra table reasoning in setting up SQL tables, can someone try to explain clearly?

You have entities: things in the physical world that you want to model with rows in your database. Those things have relationships between them.

Your three-table solution includes two entity tables (Product and Colour). And, it looks like it includes a so-called junction table that allows you to construct a many-to-many relationship between the entities. Why is this good? It makes your database a bit smaller and easier to search.

But you have correctly observed that you don't run out of "small", "red" or "t-shirt", you run out of "small red t-shirts". So, your most important entity, the one physical thing you must model correctly if you want to sell stuff, is your Stock-Keeping Unit. SKU

So your question comes down to this: What attributes do you want on your SKU entities? Maybe something like this?

SKU_id  num_in_stock  Size_id  Colour_id  Product_id  description

 1234          7         1         1         1   Small red t-shirt
 1235          0         2         1         1   Medium red t-shirt
 1248          4         3         2         2   Large green hoodie

You can search by colour, size, or product using the _id values. Or, you can just use text descriptions and search for products with slow SQL like this.

description LIKE '%green%'

Your decisions about this are driven by how you want to organize your product presentation and search functions.

huangapple
  • 本文由 发表于 2023年2月7日 05:05:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定