简体   繁体   English

表设计 - 建模层次结构问题,其中 2 1:许多实体都与第三个表相关

[英]Table design - modelling hierarchy issues where 2 1:many entities both relate to a third table

This is (I think) a generic question, but here's a specific example.这是(我认为)一个通用问题,但这是一个具体的例子。 Suppose that:假设:

  1. Albums each have 1-n Tracks .每个Albums都有 1-n Tracks
  2. Each Track is in exactly one Album .每首Track都在一个Album中。 (Even if " Logging with the Bloggs " appears in both Bloggs' First Album and Bloggs' Greatest Hit s, these are 2 entities for my purposes.) (即使“ Logging with the Bloggs ”出现在Bloggs 的 First AlbumBloggs 的 Greatest Hit中,对于我来说,这两个实体也是如此。)
  3. Each Track has exactly one Artist .每个Track都有一个Artist
  4. Artists have 1-n Tracks . Artists有 1-n Tracks
  5. Each Album has 1 Album_Artist .每张Album都有 1 个Album_Artist (Where all the Tracks are by Bloggs , the Album_Artist is Bloggs . Where the Tracks are by different Artists, as in A Collection of Great Logging Songs , the Album_Artist is Various. (所有Tracks均由Bloggs提供, Album_ArtistBloggsTracks由不同艺术家提供,如A Collection of Great Logging Songs中, Album_Artist是各种各样的。

I want to be able to answer questions like:我希望能够回答以下问题:

  • List all the Bloggs albums列出所有Bloggs专辑
  • Find all the Bloggs Tracks with ' logging ' in the title查找标题中带有“ logging ”的所有博客Tracks
  • List all the Album_Artists .列出所有Album_Artists

So what's the best table model?那么最好的表 model 是什么? Currently I have an Artists table with a boolean column for IsAlbumArtist?目前我有一个Artists表,其中包含 IsAlbumArtist 的 boolean 列? Then Tracks has a foreign key Artist_id and a foreign key Album_id.然后Tracks有一个外键 Artist_id 和一个外键 Album_id。 and Albums has a foreign key Artist_id并且Albums有一个外键 Artist_id

But would it be preferable to have a separate AlbumArtists table, or is this a moment to think about table inheritance?但是最好有一个单独的AlbumArtists表,还是现在考虑一下表 inheritance?

Thanks for any design info and tips!感谢您提供任何设计信息和提示!

-- Artist ART exists.
--
artist {ART}
    PK {ART}

-- Make sure to add "Various" to the list of artists.
-- Album ALB by album-artist ART exists.
--
album {ALB, ART}
   PK {ALB}

FK1 {ART} REFERENCES artist {ART}
-- Track number TR_NO of album ALB 
-- performed by artist ART is named TR_TITLE.
--
track {ALB, TR_NO, ART, TR_TITLE}
   PK {ALB, TR_NO}
   AK {ALB, TR_TITLE}

FK1 {ALB} REFERENCES album  {ALB}
FK2 {ART} REFERENCES artist {ART}

-- List all the Bloggs albums
--
SELECT ALB
FROM album
WHERE ART = 'Bloggs' ;
-- Find all the Bloggs tracks
-- with 'logging' in the title.
--
SELECT ALB, TR_NO, TR_TITLE
FROM track
WHERE ART = 'Bloggs'
AND TR_TITLE like '%logging%' ;
-- List all the album-artists
--
SELECT ART
FROM album ;

Note:笔记:

All attributes (columns) NOT NULL

PK = Primary Key
AK = Alternate Key (Unique)
FK = Foreign Key

This is a simplified logical design, use it as a proof of concept.这是一个简化的逻辑设计,将其用作概念验证。 To use (add)integers or codes for PKs take a look at this example .要为PKs使用(添加)整数或代码,请查看此示例

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

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