2012-03-26 57 views
-1

对于这种情况,我有两个类之间的一对多关系。我有一场游泳比赛,比赛可以有x名游泳选手。SQL未知属性数

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

+1

为什么属性的数量未知? – keyser 2012-03-26 16:58:16

+0

听起来像你需要花更多时间在应用程序的设计/体系结构方面,然后再构建表... – 2012-03-26 17:00:28

+0

您的问题不是特定的。如果你想得到答案,那么你需要具体解释你的情况。请关闭这个问题。 – Ben 2012-03-26 17:02:15

回答

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) 
)