How to structure a DB to store different value
The title could be not very precise but here is what i would like to do, and to ask for advice
I have two tables, one is dedicated to different artists, the other one for different music genres. The final idea is to assign one or more genres to one artist.
I was thinking to use tbl_genre.ID an开发者_运维知识库d to store it in tbl_artist.genre but this is not going to work (possible issues when searching for a specific genre in tbl_artist using LIKE % %).
The other option is maybe to create a 3th table and to store all the genre IDs relevant to an artist in a separate row, but somehow this solution looks "lame", and im wondering if there is a better one.
Thanks.
This is a very typical many to many relationship. Genre's have many artists and artists can have many Genre's. You need to use a third 'join' table to accomplish this. I would call it something like tbl_artists_genres and it will have just two fields, artist_id and genre_id.
Your "lame" solution is correct. You should create a table with ArtistID and GenreID and add one row to this table for each genre that an artist belongs to.
A map table is a pretty good solution (what you called "lame"). Another solution that is more denormalized is to store all the IDs of the genres in the artist table in a table field. Use a standard format such as JSON to serialize and de-serialize this field.
精彩评论