Search code examples
mysqldatabasedatabase-normalization

MySQL Database Normalization .. one table to connect multiple others?


Let's assume I have a very large database with tons of tables in it. Certain of these tables contain datasets to be connected to each other like

table: album 
table: artist
--> connected by table: album_artist

table: company
table: product
--> connected by table: company_product

The tables album_artist and company_product contain 3 columns representing primary key, albumID/artistID meanwhile companyID/productID...

Is it a good practice to do something like an "assoc" table which is made up like

 ---------------------------------------------------------
| id int(11) primary | leftID | assocType       | rightID |
|---------------------------------------------------------|
| 1                  | 10     | company:product | 4       |
| 2                  | 6      | company:product | 5       |
| 3                  | 4      | album:artist    | 10      |
 ---------------------------------------------------------

I'm not sure if this is the way to go or if there's anything else than creating multiple connection tables?!


Solution

  • No, no, a thousand times no. Don't overthink your many-to-many relationships. Just keep them simple. There's nothing to gain and a lot to lose by trying to consolidate all your relationships in a single table.

    If you have a many to many relationship between, say guiarist and drummer, then you need a guitarist_drummer table with two columns in it: guitarist_id and drummer_id. That table's primary key should be comprised of both columns. And you should have another index that's made of the two columns in the opposite order. Don't add a third column with an autoincrmenting id to those join tables. That's a waste, and it allows duplicated pairs in those tables, which is generally confusing.

    People who took the RDBMS class in school will immediately recognize how these tables work. That's good, because it means you don't have to be the only programmer on this project for the rest of your life.

    Pro tip: Use the same column name everywhere. Make your guitarist table contain a primary key called guitarist_id rather than id. It makes your relationship tables easier to understand. And, if you use a reverse engineering tool like Sql Developer that tool will have an easier time with your schema.