在这种情况下,两个类之间存在一对多关系。我有游泳比赛,那场比赛可以有x名游泳运动员。
我该如何为此创建SQL表,我知道在游泳比赛中我必须使用Swimmers的主键作为外键,但我不知道如何表示正确数量的属性,因为这是未知的。
这被称为m:n关系,通常使用映射表求解。
类似这样的东西:
create table swimmer
(
id integer not null primary key,
lastname varchar(100) not null,
firstname varchar(100)
)
create table competition
(
id integer not null primary key,
name varchar(50) not null,
comp_date date not null
)
create table participant
(
swimmer_id integer not null,
competition_id integer not null,
rank_in_competetion integer,
primary key (swimmer_id, competition_id),
constraint fk_participant_swimmer (swimmer_id) references swimmer(id),
constraint fk_participant_competition (competition_id) references competition(id)
)