SQL多值、表设计
这更多的是关于结构的问题,而不是编程语言本身的问题。我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅仅因为重要数据(如手机型号和制造商)在其他颜色下可用而重复它是不必要的冗余。
最好创建一个像
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)
你需要一个多对多的关系。您需要第三个“映射”表,称为 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.
简单标准化
您拥有的有关手机的任何其他详细信息(例如 SKU)应转到表“电话”(或“型号”,如果您想这样称呼它)。
制造商的详细信息应参见 PhoneManf 等表。
Simple normalization
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.
您可以创建另一个表:
You can just create another table:
我要做的是创建一个名为phone_color 的桥接表,其中一列用于电话,另一列用于颜色。这些列存储映射多对多关系的各自 ID。
因此,考虑到桥接表中行上方的数据,
希望有帮助。
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
Hope that helps.