SQL未知数量的属性

时间:2012-03-26 16:56:24

标签: sql foreign-keys primary-key

对于这种情况,我在两个班级之间有一对多的关系。我有一个游泳比赛,那场比赛可以有x个游泳运动员。

我如何为此创建一个SQL表,我知道我将不得不在游泳比赛中使用Swimmers的主键作为外键,但我不知道如何表示正确数量的属性,因为它是未知的。

1 个答案:

答案 0 :(得分:1)

这称为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)
)    
相关问题