简体   繁体   中英

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 .
  2. Each Track is in exactly one 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.)
  3. Each Track has exactly one Artist .
  4. Artists have 1-n Tracks .
  5. Each Album has 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.

I want to be able to answer questions like:

  • List all the Bloggs albums
  • Find all the Bloggs Tracks with ' logging ' in the title
  • List all the Album_Artists .

So what's the best table model? Currently I have an Artists table with a boolean column for IsAlbumArtist? Then Tracks has a foreign key Artist_id and a foreign key Album_id. and Albums has a foreign key Artist_id

But would it be preferable to have a separate AlbumArtists table, or is this a moment to think about table 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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