简体   繁体   English

数据库实体关系表的名称,这是一个好主意吗?

[英]Name for DB Entity Relationship Table and is it a good idea?

I'm not a DB design expert and have what I suspect is a newbie question. 我不是数据库设计专家,我怀疑是一个新手问题。 If it's better answered in another forum (or from a simple reference), please let me know. 如果在其他论坛(或简单参考)中更好地回答,请告诉我。

Given a Table "Recordings" and a table "Artists". 给出表“录音”和表“艺术家”。 Both tables have primary keys suitably defined. 两个表都具有适当定义的主键。 There is a relationship that we want to express between these tables. 我们希望在这些表之间表达关系。 Namely, An artist could have many recordings, or no recordings. 也就是说,艺术家可以有很多录音,或者没有录音。 A recording can only have 1 or 0 artists. 录音只能有1或0位艺术家。 (We could have some obscure recording with no known artist). (我们可能会有一些不知名艺术家的模糊录音)。

I thought the solution to this problem was to have a foreign key pointing to artist in the Recording Table. 我认为这个问题的解决方案是在录音表中有一个指向艺术家的外键。 This field could be null (the recording has no artist). 此字段可以为空(录制没有艺术家)。 Additionally, we should define cascading deletes, such that if an artist is deleted, all recordings that have a foreign referring to that artist, now have a foreign key of null. 另外,我们应该定义级联删除,这样如果删除了一个艺术家,所有具有外国引用该艺术家的录音现在都具有null的外键。 [I really do want to leave the actual recording when you delete the artist. [当你删除艺术家时,我真的想留下实际的录音。 Our real tables are not "artists" and "recordings" and a recording can exist without an artist]. 我们真正的桌子不是“艺术家”和“录音”,录音可以在没有艺术家的情况下存在]。

However, this is not how my colleagues have set things ups. 然而,这不是我的同事们如何设置的。 There is no foreign key column in 'Recordings', but rather an extra table 'RecordingArtist_Mapping' with two columns, 'Recordings'中没有外键列,而是带有两列的额外表'RecordingArtist_Mapping',

RecordingKey ArtistKey RecordingKey ArtistKey

If an Artist (or Recording) is removed, the corresponding entry in this mapping table is removed. 如果删除了Artist(或Recording),则删除此映射表中的相应条目。 I'm not saying this is wrong, just different to what I expected. 我并不是说这是错的,只是与我的预期不同。 I have certainly seen a table like this when one has a many-many relationship, but not the relationship I described above. 当一个人有很多关系时,我当然看到过这样的表,但不是我上面描述的关系。

So, my questions are: 所以,我的问题是:

  1. Have you heard of this way of describing the relationship? 你听说过这种描述这种关系的方式吗?
  2. Is there a name for this type of table? 这种类型的桌子有名字吗?
  3. Is this a good way to model the relationship or would be be better off with the foreign key idea I explained? 这是建立关系的好方法,还是我解释的外键概念会更好? What are the pros/cons of each? 每个的优点/缺点是什么?

My colleagues pointed out that with the foreign key idea, you could have a lot of nulls in the Recordings Table, and that this violates (perhaps just in spirit?) one of the Five Normal Forms in Relational Database Theory. 我的同事指出,根据外键的想法,你可能在录音表中有很多空值,这违反了(可能只是在精神上?)关系数据库理论中的五种正态形式之一。 I'm way out of my league on this one :) Does it violate one of these forms? 我已经离开了我的联盟了:)这是否违反了其中一种形式? Which one? 哪一个? How? 怎么样? (bonus points for simple reference to "Five Normal Forms" :) ). (奖励积分简单参考“五种正常形式”:))。

Thank you for your help and guidance. 感谢您的帮助和指导。

Dave 戴夫

On the face of it, this it simply an intersection table that allows a many-to-many relationship between two other tables. 从表面上看,它只是一个交集表,允许两个其他表之间的多对多关系。

When you find that you need one of these it is generally a good idea to consider "what does this table mean", and "have I included all the relevant attributes". 当您发现需要其中一个时,通常最好考虑“这个表的意思是什么”,以及“我是否包含了所有相关属性”。

In this case the table tells you that the artist contributed to the recording in some way, and you might then consider "what was the nature of the contribution". 在这种情况下,表格会告诉您艺术家以某种方式为录音做出了贡献,然后您可能会考虑“贡献的本质是什么”。

Possibly that they played a particular instrument, or instruments. 可能他们演奏了一种特定的乐器或乐器。 Possibly they were a conductor. 可能他们是指挥。

You might then consider whether people other than artists made a contribution to the recording -- sound engineer? 您可能会考虑除了艺术家以外的其他人是否为录音 - 音响工程师做出了贡献? So that leads you to consider whether "artist" is a good table at all, because you might instead want a table that represents people in general, and then you can relate any of them to a recording. 因此,您可以考虑“艺术家”是否是一个很好的表格,因为您可能需要一个代表一般人的表格,然后您可以将它们中的任何一个与录音相关联。 Maybe you even want to record the contribution of a non-person -- the London Symphony Orchestra, for example. 也许你甚至想记录一个非人的贡献 - 例如伦敦交响乐团。

You can even have entities that contribute in multiple ways -- guitarist, vocalist, and producer? 你甚至可以拥有多种方式贡献的实体 - 吉他手,歌手和制作人? You might also consider whether there ought to be a ranking of the contributions so that they are listed in the correct order (which may be a contractual issue). 您还可以考虑是否应该对贡献进行排名,以便以正确的顺序列出(可能是合同问题)。

This is exactly the way that contributions to written works are generally modelled -- here is a list of the contributor codes used in the ONIX metadata schema for books, as an illustrative industry example: https://www.medra.org/stdoc/onix-codelist-17.htm 这正是对书面作品的贡献通常建模的方式 - 这里是书籍的ONIX元数据模式中使用的贡献者代码列表,作为一个说明性的行业示例: https//www.medra.org/stdoc/ ONIX-编码表-17.htm

Yes, that's a viable setup, this is called vertical partitioning. 是的,这是一个可行的设置,这称为垂直分区。

Basically, you move your artist field from recording to another table with the primary key referencing that on recording . 基本上,您将artist字段从recording移动到另一个表,主键引用recording的主键。

The benefit is you don't necessarily have to retrieve artists with doing lookups on recordings, the drawback is that if you still have to, if would be somewhat slower, because of an extra join. 好处是你不一定要通过对录音进行查找来检索艺术家,缺点是如果你仍然必须,如果会有点慢,因为额外的连接。

Your solution with a foreign key in Recording is absolutely correct from the Normalization Theory point of view, it does not violate any significant normal form (the most important one are Third Normal Form, and Boyce-Codd Normal Form, and neither of them is violated). 从标准化理论的角度来看,你在录音中使用外键的解决方案是完全正确的,它不违反任何重要的正常形式(最重要的是第三范式和博伊斯 - 科德范式,并且它们都没有被违反)。

Moreover, a part being conceptually simpler and safe, from a practical point of view it is more efficient, since it in general reduces the number of joins that must be done. 此外,从概念上看,一个部件在概念上更简单和安全,它更有效,因为它通常会减少必须完成的连接数量。 In may opinion, the pros are greater than the cons. 在意见中,专业人士的利益大于利弊。

Have you heard of this way of describing the relationship? 你听说过这种描述这种关系的方式吗?

Yes, it's a many to many relationship. 是的,这是一个多对多的关系。 A recording can have more than one artist. 录音可以有多个艺术家。 An artist can have more than one recording. 艺术家可以有多个录音。

Is there a name for this type of table? 这种类型的桌子有名字吗?

I call them junction tables . 我称他们为联结表

Is this a good way to model the relationship or would be be better off with the foreign key idea I explained? 这是建立关系的好方法,还是我解释的外键概念会更好? What are the pros/cons of each? 每个的优点/缺点是什么?

A junction table is required in a many to many relationship. 在多对多关系中需要联结表。 When you have a one to many relationship, you would use a foreign key in the many table. 如果您有一对多关系,则可以在许多表中使用外键。

As far as 4th level and 5th level database normalization, this A Simple Guide to Five Normal Forms in Relational Database Theory article from 1982 explains the different levels. 就第4级和第5级数据库规范化而言,1982年的“ 关系数据库理论中的五种范式的简单指南”解释了不同的层次。

Under fourth normal form, a record type should not contain two or more independent multi-valued facts about an entity. 在第四范式下,记录类型不应包含关于实体的两个或多个独立的多值事实。

Fifth normal form deals with cases where information can be reconstructed from smaller pieces of information that can be maintained with less redundancy. 第五种常规形式处理的情况是,可以从较少的信息中重建信息,这些信息可以用较少的冗余来维护。

I remember the first 3 levels of normalization with this sentence. 我记得这句话的前三个标准化水平。

I solemnly swear that the columns rely on the key, the whole key, and nothing but the key, so help me Codd. 我庄严地发誓,这些专栏依赖于钥匙,整个钥匙,除了钥匙外,所以请帮助我Codd。

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

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