[英]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:
假设:
Albums
each have 1-n Tracks
.Albums
都有 1-n Tracks
。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.) Track
has exactly one Artist
.Track
都有一个Artist
。Artists
have 1-n Tracks
. Artists
有 1-n Tracks
。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_Artist
为Bloggs 。 Tracks
由不同艺术家提供,如A Collection of Great Logging Songs中, Album_Artist
是各种各样的。 I want to be able to answer questions like:我希望能够回答以下问题:
Tracks
with ' logging ' in the titleTracks
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.