多对多的关系 [英] many to many relationship
问题描述
如果我有两个表 - table beer
和表分配器
,每个都有一个主键和第三具有外键和调用beer_distributor的表。
If I have two tables - table beer
and table distributor
, each one have a primary key and a third table that have the foreign keys and calls beer_distributor
这个表中的新字段(主键)是否足够?另一种方式是连接,正确吗?要获得例如DUVEL De vroliijke喝酒者?
Is it adequate a new field (primary key) in this table? The other way is with joins, correct? To obtain for example DUVEL De vroliijke drinker?
推荐答案
我将把一个主键放在连接表中 beer_distributor
,而不是两个外键的双主键。 IMO,当维持关系时,它使生活更容易。
I would put a primary key in the join table beer_distributor
, not a dual primary key of the two foreign keys. IMO, it makes life easier when maintaining the relationship.
要强调这一点,为啤酒 163
更改分销商 ACOO9
。使用双主键,您必须删除然后重新插入OR或知道两个现有值以更新记录。使用单独的主键,您只需使用此值更新记录。当在这些数据上构建应用程序时很方便。如果这是严格的数据仓库,那么双主键可能从DBA的角度更有意义。
To emphasize this point, consider having to change the distributor ACOO9
for beer 163
. With the dual primary key, you'd have to remove then reinsert OR know both existing values to update the record. With a separate primary key, you'd simply update the record using this value. Comes in handy when building applications on top this data. If this is strictly a data warehouse, then a dual primary key might make more sense from the DBA perspective.
UPDATE beer_distributor SET distributor_id = XXXXX WHERE beer_id = 163 AND distributor_id = AC009
对比
UPDATE beer_distributor SET distributor_id = XXXXX WHERE id = 1234
这篇关于多对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!