Why does the m: n relationship in the DB design have to create a new relation?

梦想的初衷 提交于 2019-12-25 17:45:26

问题


I recently learned about DB design. However, in the m: n relation, we need to create a new relation. I do not understand this reason well. I also wonder why I place a foreign key in n relations instead of 1 when I have a 1: n relationship.


回答1:


Originally, Chen's method for mapping the entity-relationship model to the relational model prescribed that every relationship map to a separate relation (table). However, it's become common practice to denormalize one-to-one and one-to-many relationship relations into entity relations with the same determinant/key, to limit the number of tables in a database. The persistence of network data model thinking (records with references) also helps to popularize this approach. However, not all relationship relations can be denormalized - M:N binary as well as ternary and higher relationships have keys that involve multiple entity sets, and so necessarily require their own relations/tables.

For example, consider the following conceptual models:

A direct mapping of each entity relation and relationship relation to separate tables would give the following physical models:

Note that the one-to-one relationship Manages has two candidate keys, but the physical schema requires that we choose one as primary key. I chose department_id since denormalization of the relationship relation into the Employee entity relation would require a nullable column (since not every Employee is a manager).

More importantly, note that the N:1 and 1:1 relationship tables have the same keys as one of the entity tables. We can take advantage of this to combine those relations:

However, the M:N and M:N:P relationships have composite keys, and can't be combined with other tables (unless, in a larger model, you coincidentally have multiple relationships with the same cardinalities between the same entity sets).

I also wonder why I place a foreign key in n relations instead of 1 when I have a 1: n relationship.

It's more correct to say that a relationship is determined by the combination of its independent entity sets. Values of these sets can appear any number of times, as indicated by M, N, etc. We indicate dependent entity sets with a 1 - these act similar to attributes, in that each unique combination of values in the independent roles determines a single value for each dependent role. This is the logical concept of functional dependency.

When we denormalize tables as above, we do so based on matching keys, which means only dependent columns get transferred from one table to another. In the end, it may look like we added a single FK column to the entity table on the many side of the relationship, but we actually added a mapping from the key to the dependent column, and we just didn't need to repeat the key.



来源:https://stackoverflow.com/questions/44050260/why-does-the-m-n-relationship-in-the-db-design-have-to-create-a-new-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!