简体   繁体   English

MYSQL表设计帮助

[英]MYSQL Table design help

I have a table called teams (id, name, ...) I have a table called divisions (id, name, ......) I want the diviosn to hold the teams.id for each team in a divsion and Teams can be in more than one divsion. 我有一个表格,称为teams(id,name,...)。我有一个表格,称为divisions(id,name,......)。我希望diviosn将每个团队中的teams.id保持为div。团队可以分成多个区域。 How do I set this up? 我该如何设置?

Assuming your tables looked like this 假设你的桌子看起来像这样

teams
-teamId
-teamName

divisions
-divisionId
-divisionName

I think what you are looking for is a many-to-many link table 我认为您正在寻找的是多对多链接表

membership
-divisionId
-teamId

This would allow you to find all the teams in a division with 这样您就可以找到一个部门中的所有团队

select teamId
from team t
join membership m
  on m.teamId = t.teamId
where m.divisionId = 'theIdYouWant'

It would also allow as many teams as you want to belong to a division and as many divisions as you want to contain the same teams. 它还将允许您要属于一个部门的团队数量尽可能多,而要包含相同团队的团队数量则要尽可能多。 It's a many to many relationship table. 这是一个多对多关系表。

这是用于多对多关系的标准方法:制作一个单独的表teams_divisons(或divisions_teams)表,其中包含两列team_id和division_id。

You create what's called a relationship table. 您创建所谓的关系表。 It's an intermediary table between the Teams tables and the Divisions table. 这是Teams表和Divisions表之间的中间表。 Then we say that there is a many-to-many relationship between the Teams table (tbl_teams) and Divisions table (tbl_divisions). 然后我们说在Teams表(tbl_teams)和Divisions表(tbl_divisions)之间存在多对多关系。 You can have many teams in one division, one team in many divisions, or many teams in many divisions. 您可以在一个部门中拥有多个团队,在多个部门中具有一个团队,也可以在多个部门中具有多个团队。

tbl_teams_divisions_rel tbl_teams_divisions_rel

  • ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT ID INT主键不能为空AUTO_INCREMENT

  • Team_ID INT NOT NULL Team_ID INT非空

  • Division_ID INT NOT NULL Division_ID INT非空

Then you establish a relationship between tbl_teams_divisions_rel.Team_ID and tbl_team.ID, and then between tbl_teams_divisions_rel.Division_ID and tbl_divisions.ID 然后,您在tbl_teams_divisions_rel.Team_ID和tbl_team.ID之间,然后在tbl_teams_divisions_rel.Division_ID和tbl_divisions.ID之间建立关系。

If you ask the question, how to list all teams in a particular division with ID 13, you would do the following: 如果您提出问题,如何列出ID为13的特定部门中的所有团队,您将执行以下操作:

SELECT tbl_teams.*
FROM
    tbl_teams
    INNER JOIN tbl_teams_divisions_rel ON tbl_teams_divisions_rel.Team_ID = tbl_teams.ID
WHERE tbl_teams_divisions_rel.Division_ID = 13

Hope it helps! 希望能帮助到你!

You could add two columns - division_names to teams which will contain the names of teams that are in each division, and team_names to divisions which will contain the names of the divisions that team is a part of. -你可以添加两列division_names到团队将包含团队,在各部门的名称,并team_names到将包含团队的一部分区划名称部门。

When you add a team or division, follow it by a ; 当您添加团队或部门时,请紧跟一个; , then you can create an array of teams or divisions based on that field by using $field = explode(";", $field); ,那么您可以使用$field = explode(";", $field);基于该字段创建一组团队或部门$field = explode(";", $field); , and then list the teams or divisions using foreach() . ,然后使用foreach()列出团队或部门。

That's assuming you're using PHP. 假设您正在使用PHP。 Let me know if I misunderstood you. 让我知道我是否误解了你。

You need to create a many-to-many junction table to join division and team tables. 您需要创建一个多对多联结表才能联接部门和团队表。

CREATE TABLE division (division_id INT NOT NULL,
                     PRIMARY KEY (division_id)
);

CREATE TABLE team (team_id INT NOT NULL,
                     PRIMARY KEY (team_id)
);

CREATE TABLE division_team (division_team_id INT, 
                    PRIMARY KEY (division_team_id)
                    division_id INT, 
                    team_id INT,
                    FOREIGN KEY (division_id) REFERENCES division(division_id),
                    FOREIGN KEY (team_id) REFERENCES team(team_id),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM