2015-12-20 129 views
1

新手在SQL這裏。我該如何處理這種關係?

我這有關係:

relationship

團隊必須全部5名成員引用和每個玩家都有提及他的團隊。現在,我是否也應該製作teamPlayer1,2,...一個PLAYERS實體的FK?因爲到目前爲止,我只與一個屬性相對應的關係一起工作,而不是五對一。不知道如何去做這件事。

+0

如果你想保證引用完整性,那麼你應該在player表中的每個teamPlayer列和playerId之間有一個FK關係。 – DeanOC

+0

不知道如何在我的建模軟件中去解決這個問題。我所能做的就是擁有1-1個1-many-1許多關係。如何實現這一目標? – user1255410

+0

您需要添加更多上下文(或標籤)。你的建模軟件是什麼?你的數據庫是什麼? – DeanOC

回答

1

teamPlayer1-5是多餘的,應該刪除。您可以通過連接來重建玩家列表。如果你想每隊只允許5名球員,請用teamMember int, UNIQUE(teamId, teamMember), CHECK(teamMember between 0 and 4)補充球員。

更正:因爲所需信息都在PLAYERS表中,所以您可以在沒有連接的情況下爲每個團隊重建球員。

0

我建議你創建三個表格:teams,players,team_players

Teams

create table teams (
    id int not null auto_increment, 
    teamname varchar(100) not null, 
    country varchar(100) not null, 
    primary key (id) 
); 

Players

create table players (
    id int not null auto_increment, 
    firstname varchar(100) not null, 
    lastname varchar(100) not null, 
    nickname varchar(100), 
    country varchar(100) not null, 
    fieldposition varchar(100), -- can be an int FK to a fieldpositions table 
    debutdate date, 
    primary key (id) 
); 

Team_players

create table team_players (
    id int not null auto_increment, 
    teamid int not null, 
    playerid int not null, 
    primary key (id), 
    constraint uk_team_players_rel unique (teamid, playerid), 
    constraint fk_team_players_teamid foreign key (teamid) references teams (id), 
    constraint fk_team_players_playerid foreign key (playerid) references players (id) 
); 

實施例(MySQL的):http://sqlfiddle.com/#!9/7c4ff8

相關問題