1

I'm working on a database project about music and albums in MySQL, where i make a list over some popular artists, their most sold album, and the songs contained within them. But i suddenly got really uncertain of what to do when it comes to filling in the name of the songs for each album. Should i make an individual table for each list of songs, or should all the songs (about 50 of them in total from all the albums) from all the different artists (5 different artists) be filled inn in the same table (i'm eventually gonna export the data and connect it to a PHP folder

Hope the question was clear

2 Answers 2

2

All the songs should be in one "Songs" table. Then you create a column "Album ID" in that table which is a foreign key back to the ID column in the albums table. This is how you know which song belongs to which album. (And of course you have the same kind of relationship between "album" and "artist".)

This is called a "one-to-many" relationship and is one of the basic principles of relational database design.

If you ever find yourself creating multiple tables to represent the same kind of data item, you know you've gone wrong.

N.B. If you want to support the idea that the same song (or track probably, to be more accurate, since many different recordings of a song could potentially be made) can be included on multiple albums, then you'll need to implement a "many-to-many" relationship where you have an extra table in between "albums" and "songs" which holds Album ID and Song ID. Each would be a foreign key back to the Albums and Songs tables, respectively. And to ensure no duplication, both fields would be specified as a Compound Primary Key. That way you can list the same Song ID in that table many times against different albums. Same again if you want to have that flexibility in the relationship between "artists" and "albums".

This might be a good time to take a break and study relational database design and data normalisation concepts in some more detail, then you can start to see these patterns for yourself and make the right decision in your schema designs.

5
  • @Zim84 True. That was somewhat implied in the question I think, it seems to be a fairly simple example rather than a full-on real-world thing full of multi-artist compilations etc. But I take your point.
    – ADyson
    Feb 27, 2019 at 12:55
  • Why? In the album table several albums could link to the same song id. I would do it this way rather than the other way round (linking in the songs table to the album table).
    – Axel
    Feb 27, 2019 at 12:55
  • 1
    @Axel actually I just edited the answer to talk about the scenario where the same track can appear on multiple albums. This requires a many-to-many relationship. I'm not quite sure what you mean when you say doing it the "other way round"? If you make "song ID" a foreign key of "albums" instead, then the song can appear on multiple albums, but each album can only contain one song. I assume you wouldn't really think that's desirable.
    – ADyson
    Feb 27, 2019 at 13:00
  • 1
    @ADyson I have to admit that my logic was flawed here for a moment. You are right that actually entries in both tables need to be able to point to multiple entries in the other table, thus a third table is needed.
    – Axel
    Feb 27, 2019 at 13:03
  • 1
    I really appreciate the helpful answers, and i think i should be able to continue the project :D.
    – Subaru10
    Feb 27, 2019 at 13:11
0

Similarly to this question about databases for playlists you should also use one table for the albums and one table for the songs.

Additionally you might also need a table for artists, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.