简体   繁体   English

如何设置具有多个相似属性的数据库表

[英]How to setup database tables with multiple similar attributes

There are products, sections and attributes. 有产品,部分和属性。

Each attribute can have up to 5 or 6 options. 每个属性最多可以有5个或6个选项。

Example: Power 示例:电源

10 Volt
15 Volt
20 Volt

And there are about 10 products in total, each product has up to 17 attributes applied to it. 总共大约有10个产品,每个产品最多应用17个属性。

Example: Product 1 示例:产品1

power - 10 volt
color - red, yellow
link - online, offline

How would you setup the tables? 您将如何设置表格? Im stumped. 我很沮丧 I was thinking of having a separate table for each attrubute, then a products table and sections table. 我当时在考虑为每个属性分配一个单独的表,然后是一个产品表和一个部分表。

The products table would house foreign keys for the attributes that relate to it and sections. 产品表将容纳与它和各节相关的属性的外键。 Does this make sense? 这有意义吗?

I was thinking of having a separate table for each attrubute, 我当时在考虑为每个人物准备一个单独的桌子,

Don't create a separate table for each attribute, this won't server your purpose. 不要为每个属性创建单独的表,这不会满足您的目的。

Create separate tables for each of the products, sections and attributes and join them accordingly using PKs and FKs. 为每个产品,部分和属性创建单独的表,并使用PK和FK相应地将它们联接。

This is quite common. 这很普遍。 You can have a base product table with some general attributes like... 您可以拥有一个基本产品表,其中包含一些常规属性,例如...

Product
-------
ProductID
ProductName 
Description
Price

Then you can extend the table for different product types. 然后,您可以将表格扩展为其他产品类型。 So you could have a table like 所以你可以有一张桌子

ProductElectrical 
-----------------
ProductID
Voltage
Watts
BatterySize

Or... 要么...

ProductApparel
---------------
ProductID
Color
Size
Material

Then when needed you can join the extension tables to the core product table like this... 然后,在需要时,您可以像这样将扩展表连接到核心产品表...

select p.ProductID, p.ProductName, p.Description, pa.Color, pa.Size, pa.Material
from Product p
join ProductApparel pa
on pa.ProductID = p.ProductID
where pa.Size = "XXL"

This way you can have a compact core product table used throughout your system. 这样,您可以在整个系统中使用紧凑的核心产品表。 When you need to see the extra attributes you can easily join them to the core Product table. 当您需要查看额外的属性时,可以轻松地将它们加入核心产品表。

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

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