简体   繁体   English

数据库设计

[英]Database design

I am building a music streaming site, where users will be able to purchase and stream mp3's. 我正在建立一个音乐流媒体网站,用户可以在其中购买和流媒体mp3。 I have a subset entity diagram which can be described as follows: 我有一个子集实体图,可以描述如下:

ER图

I want to normalise the data to 3NF. 我想将数据标准化为3NF。 How many tables would I need? 我需要多少张桌子? obviously I want to avoid including partial dependancies, which would require more tables than just album, artist, songs - but I'm not sure what else to add? 显然,我想避免包括部分相依性,这不仅需要专辑,艺术家,歌曲,还需要更多的表,但是我不确定还需要添加什么? Any thoughts from experience? 经验中有什么想法吗?

Well, you've done the ER level. 好了,您已经完成了ER级别。 You need to identify Keys and Attributes before you can work out Functional Dependencies. 您需要先确定键和属性,然后才能计算出功能依赖性。 There is a fair amount of work to do before you get to 3NF. 进入3NF之前,您还有很多工作要做。 Eg. 例如。 Song Titles are duplicated. 歌曲标题重复。

Also, there are questions: 另外,还有一些问题:

  • is the site selling Albums, Songs, or both ? 该网站销售专辑,歌曲或两者兼而有之吗? (I've modelled both) (我都建模)
  • if both, how do you track a sale or download ? 如果同时存在,您如何跟踪销售或下载?
  • do you care about the same Song title recorded by different Artists ? 您是否关心不同歌手录制的同一首歌标题?

Anyway, here is a resolved ▶Entity Relation Diagram◀ , at least for the info provided. 无论如何,至少在提供的信息上,这是一个已解析的“ 实体关系图” It is closer to 5NF than 3NF, but I cannot declare it as such, because it is not complete. 它比3NF更接近5NF,但是我不能这样声明,因为它不完整。

Readers who are unfamiliar with the Standard for Modelling Relational Databases may find ▶IDEF1X Notational◀ useful. 不熟悉关系数据库建模标准的读者可能会发现▶IDEF1X Notational◀非常有用。

It uses a simple Supertype-Subtype structure, the Principle of Orthogonal Design. 它使用简单的Supertype-Subtype结构,即“正交设计原理”。 The Item that is sold ie either an Album xor a Song. 出售的商品,即专辑或歌曲。

Feel free to ask clarifying questions. 随时提出澄清的问题。

You will need 4 tables: Artists, Songs, Albums, and AlbumSongs. 您将需要4个表格:艺术家,歌曲,专辑和专辑歌曲。
The last one is required since the exact same song (=same edit/version...) could be included in several albums, so you have there a m-to-m relationship. 最后一首是必需的,因为完全相同的歌曲(=相同的编辑/版本...)可以包含在几张专辑中,因此您之间存在m-m的关系。

I agree with iDevelop but with 1 extra table. 我同意iDevelop,但有1张额外的桌子。 Here is how I would model it. 这是我要建模的方式。

Tables: Artist, Song, Album, AlbumSongMap, SingleInfo 表格:艺术家,歌曲,专辑,专辑歌曲地图,SingleInfo

If the song was a released as a single on a different date, you can get that from SingleInfo. 如果歌曲是在其他日期作为单曲发行的,则可以从SingleInfo获得。 The single may have been released with some cover art that is different from the album art. 该单曲可能已经发行,发行时带有一些与专辑封面不同的封面。 You would store the singles art in SingleInfo. 您会将单曲作品存储在SingleInfo中。 MAYBE a song can be released as a single multiple times, with new cover art or something so it could possibly be a 1-many relation. 也许一首歌可以多次发行,并带有新的封面或其他内容,因此可能是一对多的关系。 Otherwise it is 1-1. 否则为1-1。

If you can join Song with SingleInfo that means it was released as a single. 如果您可以将Song与SingleInfo结合使用,则意味着它已作为一个单独的发行。 If you can join Song with Album (using the map) then you will find all the album's it was released under. 如果您可以将Song和Album一起使用地图(使用地图),那么您将找到该专辑下发布的所有专辑。

A digital enhancement to an old song is a new song. 对旧歌曲的数字增强是新歌曲。 (or at least a different binary). (或至少是其他二进制文件)。 You may want to further normalize Song to allow storage of digital enhancements without duplicating songName, etc. 您可能需要进一步规范化Song以允许存储数字增强功能而无需复制songName等。

When you switch over from ER modeling to relational modeling (tables), you need one table for each entity. 从ER建模切换到关系建模(表)时,每个实体都需要一个表。 You also need a table for some relationships. 您还需要一些关系表。

In the diagram you've given us, both relationships are many to one. 在您给我们的图中,两种关系都是一对多的。 Many to one relationships do not require a table. 多对一关系不需要表。 You can get away with adding foreign keys to entity tables. 您可以通过将外键添加到实体表来摆脱困境。 Therefore the answer to your question is 3 tables: Artists, Albums and Songs. 因此,您的问题的答案是3张桌子:艺术家,专辑和歌曲。

However , I question your ER diagram. 但是 ,我质疑您的ER图。 It seems to me that the "contains" relationship is really many to many. 在我看来,“包含”关系真的是很多对很多。 An album clearly contains many songs. 一张专辑显然包含许多歌曲。 But a given song can appear on more than one album. 但是给定的歌曲可以出现在多个专辑中。 So there should be an arrowhead on the line that connects "contains" to "album". 因此,在连接“包含”与“专辑”的线上应该有一个箭头。

If you accept this revision to your ER model, then the number of tables increases to 4: Artists, Albums, Songs, and Contains. 如果您接受此ER模型的修订,则表的数量将增加到4:艺术家,专辑,歌曲和包含。

A similar argument might be made for Artist and Song. 艺术家和宋可能会提出类似的论点。 If two artists collaborate on a single song, (eg Dolly Parton and Kenny Rogers singing "Islands in the Stream" together, then you might want to model "produces" as a many to many relationship. Now you need 5 tables: Artists, Albums, Songs, Contains and Produces. 如果两个艺术家合作创作一首歌曲(例如Dolly Parton和Kenny Rogers一起演唱“ Islands in the Stream”,那么您可能希望将“农产品”建模为多对多关系。现在,您需要5张桌子:艺术家,专辑) ,歌曲,包含和产生。

Artists, Albums, and Songs are going to require a PK that identifies the corresponding entity. 艺术家,专辑和歌曲将需要一个用于标识相应实体的PK。 Entity integrity demands that the correspondence bewteen entity instances and table rows be one-to-one. 实体完整性要求实体实例和表行之间的对应关系是一对一的。

The Contains and Produces tables can be built without a separate Id attibute. 可以在没有单独的ID属性的情况下构建“包含”和“产生”表。 You will need a pair of FKs in each of these tables, and you can declare a compound PK for each table consisting of the two FKs. 每个这些表中都需要一对FK,并且可以为由两个FK组成的每个表声明一个复合PK。

Referential integrity demands that you enforce the validity of FK references, either in your programs or by declaring a references constraint in the DB. 引用完整性要求您在程序中或通过在数据库中声明引用约束来强制FK引用的有效性。 I strongly prefer declaring the constraint in the DB. 我强烈希望在数据库中声明约束。

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

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