SQL多值、表设计

发布于 2024-10-18 00:29:20 字数 559 浏览 2 评论 0原文

这更多的是关于结构的问题,而不是编程语言本身的问题。我对 SQL 有非常基本的了解。

如果您有一个包含产品的表格,并且每个产品可以有多种颜色或没有颜色。

Table: Color
Columns: Id, ColorName

1, Black
2, White  
3, Purple  
4, Red

然后我们有

Table: Phone
Columns: Id, PhoneName, PhoneManf, ColorID

例如。 Phone Table 可能有一款 HTC EVO,有黑白两种颜色,

这意味着每部手机的每种颜色都会有一个记录。 或者

1, EVO, HTC, 1
2, EVO, HTC, 2
3, Curve, Blackberry,1
4, Curve, Blackberry,3

有没有办法为每部手机保留 1 条记录,其中颜色键就像一个数组?

抱歉,如果这是一个非常基本的问题,我对此很陌生,并且想确保我的出发点是正确的。

This is more of a question about structure more than the programming language itself. I have a very basic knowledge of SQL.

If you have a table with products and each product can have multiple or no colors.

Table: Color
Columns: Id, ColorName

1, Black
2, White  
3, Purple  
4, Red

and then we have

Table: Phone
Columns: Id, PhoneName, PhoneManf, ColorID

eg. Phone Table could have a HTC EVO that is available in black and white

would this mean that for every color of every phone there would be a record.
i.e.

1, EVO, HTC, 1
2, EVO, HTC, 2
3, Curve, Blackberry,1
4, Curve, Blackberry,3

Or is there a way to have 1 record for each phone where the color key is like an array ?

Sorry if this is a very basic question, I am newish to this and want to make sure I am starting off right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

孤蝉 2024-10-25 00:29:20

仅仅因为重要数据(如手机型号和制造商)在其他颜色下可用而重复它是不必要的冗余。

最好创建一个像 phone_color (IdPhone, IdColor) 这样的表

Duplicating essential data (like phone model and manufacturer) just because it is available under other colors is unecessary redundancy.

Better create a table like phone_color (IdPhone, IdColor)

恏ㄋ傷疤忘ㄋ疼 2024-10-25 00:29:20

你需要一个多对多的关系。您需要第三个“映射”表,称为 PhoneColor。请参阅 http://www.tonymarston.net/php-mysql/many- to-many.html 了解更多信息。

You need a many-many relationship. You need a 3rd "mapping" table called PhoneColor. See http://www.tonymarston.net/php-mysql/many-to-many.html for more insight.

梦幻的味道 2024-10-25 00:29:20

简单标准化

Table: Color
Columns: ColorId, ColorName
Primary Key: ColorId

// table for the many-to-many relationship
// between Colors and Phones
Table: PhoneColor
Columns: PhoneId, ColorId
Primary Key: (PhoneId, ColorId)
Foreign Key: PhoneId
Foreign Key: ColorId

Table: Phone
Columns: PhoneId, PhoneName, PhoneManfId
Primary Key: PhoneId
Foreign Key: PhoneManfId

Table: PhoneManf
Columns: PhoneManfId, PhoneManfName, PhoneManfAddress
Primary Key: PhoneManfId

您拥有的有关手机的任何其他详细信息(例如 SKU)应转到表“电话”(或“型号”,如果您想这样称呼它)。

制造商的详细信息应参见 PhoneManf 等表。

Simple normalization

Table: Color
Columns: ColorId, ColorName
Primary Key: ColorId

// table for the many-to-many relationship
// between Colors and Phones
Table: PhoneColor
Columns: PhoneId, ColorId
Primary Key: (PhoneId, ColorId)
Foreign Key: PhoneId
Foreign Key: ColorId

Table: Phone
Columns: PhoneId, PhoneName, PhoneManfId
Primary Key: PhoneId
Foreign Key: PhoneManfId

Table: PhoneManf
Columns: PhoneManfId, PhoneManfName, PhoneManfAddress
Primary Key: PhoneManfId

Any other details you have regarding phones, like SKU should go to table Phone (or Model if you want to call it that way).

Details for Manufacturers should go to table PhoneManf, etc.

寄居者 2024-10-25 00:29:20

您可以创建另一个表:

Table: PhoneColor
Columns: PhoneId, ColorId

You can just create another table:

Table: PhoneColor
Columns: PhoneId, ColorId
简单爱 2024-10-25 00:29:20

我要做的是创建一个名为phone_color 的桥接表,其中一列用于电话,另一列用于颜色。这些列存储映射多对多关系的各自 ID。

因此,考虑到桥接表中行上方的数据,

1,1
2,2
3,1
4,3

希望有帮助。

What I would do is create a bridging table called phone_color with one column for phone and another for color. These columns store the respective ids mapping the many-to-many relationship.

So given the data above your rows in the bridging table would be

1,1
2,2
3,1
4,3

Hope that helps.

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