简体   繁体   English

在MySQL表的单元格中放置多个记录

[英]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 . 加:我只想使用MySQL功能。 don't like to seprate them with "," or other signs and let my PHP app check all rows 不喜欢用“,”或其他符号分隔它们,然后让我的PHP应用程序检查所有行

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). 您有mn个关系,这意味着一位歌手可以拥有多首歌曲,但一首歌曲可以拥有更多歌手(您指出的问题)。 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. 因此,歌曲和歌手之间的链接不能像您建议的那样位于歌曲表中,也不能位于artists表中-每行只能有一个记录。

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 . 创建一个名为新表artist_song只有两个领域artist_id, song_id

Then remove field related_artist . 然后删除字段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. Mysql支持Set as datatype( mysql set数据类型是有限的并且是固定的),其中postgre和oracle具有array / varray(更灵活)作为数据类型。

Secondly, I once used orm(hibernate) to solve this problem using a wrapper class for that specific table. 其次,我曾经使用orm(hibernate)通过对该特定表使用包装器类来解决此问题。 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 我很确定从数组类型进行引用会损害数据库的原子性。您可以在oracle中将引用数组作为外键查看

So its not possible in mysql of what you want(unless ...).Solution of Tomas is the best one still. 所以它不可能在您想要的mysql中(除非...)。Tomas的解决方案仍然是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM