简体   繁体   中英

Put more than one record in a cell in MySQL table

I want to design a database with two table, related to each other.
But I can't relate one row to more than one row in another table.
PLUS: I just want use MySQL ability . don't like to seprate them with "," or other signs and let my PHP app check all rows

this pic might help you to understand the problem :

在此处输入图片说明

You have mn relation, which means that one artist can have more than one song, but also one song can have more artists (the problem you point out). So the link between songs and artists cannot be in the songs table as you suggest, neither in the artists table - you can have only one record per row.

You need another table that relates songs and artists:

song_id  artist_id
1        2
2        2
...
6        2
6        4
...

Create a new table called artist_song with only two fields artist_id, song_id .

Then remove field related_artist .

Now you can store more than one related records in this new table.

First of all you need to store array/set in a table which will work as foreign key. Mysql supports Set as datatype( mysql set datatype which is limited and kinda fixed) where postgre and oracle have array/varray(more flexible) as datatype.

Secondly, I once used orm(hibernate) to solve this problem using a wrapper class for that specific table. I am pretty sure that referencing from a array type will hurt the atomicity of DB.You can see this for reference array as foreign key in oracle

So its not possible in mysql of what you want(unless ...).Solution of Tomas is the best one still.

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