我如何创建这个协会
我有以下 2 个 sql server 表
Products
locationCode (PK), prodId (PK), productName
---------------------------------
AUG, 1, Widget
ATL, 1, Widget
Categories
prodId (PK) catId (PK), catName
----------------------------------
1, 1, WidgetsCategory
1, 1, WidgetsCategory
我需要做什么来创建一个关联,其中单个产品可以在给定所需字段和(PK)主键的情况下拥有多个类别?
i have the following 2 sql server tables
Products
locationCode (PK), prodId (PK), productName
---------------------------------
AUG, 1, Widget
ATL, 1, Widget
Categories
prodId (PK) catId (PK), catName
----------------------------------
1, 1, WidgetsCategory
1, 1, WidgetsCategory
What would I need to do to create an association where a single product can have many categories given the required fields and (PK) Primary keys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
再创建一张将产品与类别相关联的表。
该表应包含产品的键和外键(类别 ID)。
在您原来的问题中,您没有提及位置代码是否重要。您也没有提及这是 1-N 还是 MN 类型的关系。
提供更多详细信息以获得更多帮助。
当您规范化表时,您确保每个表都具有仅与自身相关的字段(当然,除非您正在创建该关系并存储用于建立该关系的键)。
您的类别表应仅包含类别,并且不应与产品有任何关系。建立类别实体后,您可以通过另一个表将该类别与另一个产品相关联。
Create one more table that relates products to categories.
This table should contain the key of products and a foreign key (category id).
In your original question you don't mention whether the location code matters or not. You also don't mention if this is a 1-N or M-N type relationship.
Provide a bit more detail to get additional help.
When you normalize your tables you ensure every table has fields related with only itself (unless of course you are creating that relationship and you store the keys to make that relationship).
Your categories table should only include categories, and should not have anything to do with a product. Once you establish a Category entity, you then relate that category to another product, via another table.