Search code examples
database-designmany-to-manynaming

Naming many to many relationship which table first?


I've read this: https://stackoverflow.com/questions/631850/how-do-you-name-your-many-to-many-relationship-tables But my question is a little different.

I'm just wondering how to know which table should be named first for the table name in a many to many relationship. In my relationship, I have artist_song. One artist can sing many songs and one song can be sung by many artists, but how to do I know whether to name it artist_song vs song_artist? Although I don't think this makes a difference, but is there a certain rule to follow for which table is named first? In my case, I used artist first, because it's more often that one artist can have many songs. It'll be more rare that one song is sung by many artists.

In this post: MySQL: How to store/retrieve artist information? they use song_artist instead, but wouldn't it make more sense naming it the other way? Or does it really not matter and is just a preference. I also heard of people just placing them in alphabetical order.


Solution

  • I don't think it matters -- since it's many-to-many, there's not a consistent way to indicate which is the "owner". But instead of just going with whichever one "feels" right, I'd recommend just sticking to a defined way and just noting it in your org's style recommendations doc.

    Alphabetical works great because it's simple, well-known, and has the advantage of aiding a person who is trying to look up the table and knows the two actors involved.