简体   繁体   English

试图了解各种表之间的数据库关系方法

[英]Trying to understand DB relationship approach between various tables

Need to know DB stucture accurate or not? 是否需要了解DB结构的准确性? Lets say there's customers / products / product_events 可以说有客户/产品/ product_events

Where each customers can have multiple products, but a product can not have more than one customer, also each products can have multiple events 每个客户可以有多个产品,但一个产品不能有一个以上的客户,每个产品可以有多个事件

So here's what I have for example 所以这就是我的例子

customers           products                            product_events
-----------     ------------------------------      ------------------------
id | name           id | customer_id | name             id | product_id | event_title

as I guess this is one => many relationship 我猜这是一个=>许多关系

but here can be large number of products and some of them or lets say many of them (products) might not be assigned to customers (in other word not sold to customers) 但是这里可能有大量的产品,其中有些甚至可以说其中的许多(产品)可能没有分配给客户(换句话说,不是卖给客户)

So to keep list of products and customer's products' list seprate is that a good approach to have any intermediary table to define products' relationship to customer? 因此,要使产品清单和客户产品清单保持分开是一种很好的方法,可以使用任何中介表来定义产品与客户的关系吗?

So how about this stucture 那这个结构怎么样

customers           products        customer_products                   
-----------     ---------------     ------------------------------      
id | name           id | name       id | customer_id | product_id   

product_events
---------------------
id | product_id | event_title

For most of usage of these structures would be fetch list of products events by particular customer 对于这些结构的大多数用法,将由特定客户获取产品事件列表

Having a customer_id column in the products table would mean that only one customer could have a particular product - eg, you and I could not both buy the same model laptop, which just doesn't sound right. products表中有一个customer_id列意味着仅一个客户可以拥有特定产品-例如,您和我不能同时购买同一型号的笔记本电脑,这听起来似乎不正确。

Having an intermediate customer_products table sounds like the right way to go. 拥有一个customer_products中间表听起来很正确。

By considering "Customer" and "PRODUCT" tables there are Many to Many relation ship between two table. 通过考虑“客户”和“产品”表,两个表之间存在多对多关系。 therefore you need to create 3rd table to maintain relationship. 因此,您需要创建第3个表来维护关系。 that 3rd Table should include two Primary key of both relation tables and Other attributes that bone because of relationship. 该第三表应包括两个关系表的主键和由于关系而骨骼化的其他属性。 3rd table primary key should be combination of both key from relation tables. 第三表主键应该是关系表中两个键的组合。

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

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