sqlite 我应该如何在SQL数据库中布局表以在音乐会上显示多个艺术家?

vlurs2pr  于 2023-08-06  发布在  SQLite
关注(0)|答案(1)|浏览(127)

我正在制作一个我在SQLite上参加过的音乐会的数据库。我设置了以下表格:
1.场地
1.艺术家
1.音乐会
如何使concert表可以有多个与之关联的艺术家ID?ArtistId1、ArtistId2、ArtistId3等是否应该只有多个列?
或者,我需要第三个表来列出每个ArtistId和相应的ConcertId吗?
我试过寻找方法,把多个艺术家ID的在同一列分隔不知何故,但不确定这是一个选项。

ykejflvf

ykejflvf1#

当你有一个多对多关系(一个音乐会可以有很多艺术家,一个艺术家可以在很多音乐会上表演),你需要一个associative entity,它至少有两个表的外键:

create table concert_artists (
    concert_id int not null references concerts,
    artist_id int not null references artists
)

字符串
这样的表也可以存储关于关系的信息,例如,您可以添加表演的表演编号(第一幕为1,依此类推)以及艺术家表演的分钟数:

create table concert_artists (
    concert_id int not null references concerts,
    artist_id int not null references artists,
    performance_number int,
    performance_duration_minutes int
)


旁注:最好用单数命名表名,例如venueartistconcert(而不是您命名的复数)。

相关问题