简体   繁体   English

如果我想从另一个表中获取 3 个不同的 Id(带有其他信息),是否需要在一个表中放置多个 Id?

[英]Do I need to put multiple Id's in a table if I want to get 3 different Id's(with other info) from another table?

I am learning about SQL and tables, foreign keys, etc. To try it instead of only learning it I created 2 tables:我正在学习 SQL 和表、外键等。为了尝试而不是只学习它,我创建了 2 个表:

Table 1: Team :表 1: Team

团队表

Table 2: Karakter (that is "character" in Dutch)表 2: Karakter (即荷兰语中的“字符”)

卡拉克特桌

A team has an Id ( TeamId ), UserId ( GebruikerId , this is not important for now), name ( Teamnaam ) and CharacterId ( KarakterId ).一个团队有一个 Id ( TeamId )、UserId ( GebruikerId ,目前这并不重要)、 name ( Teamnaam ) 和 CharacterId ( KarakterId )。

I want a team to exist of 3 characters ( karakters ), and I know I can find character via foreign keys, but sins I want 3 characters and not only 1 I was wondering if in my table TeamI need to put KarakterId1, KarakterId2, KarakterId3?我想要一个由 3 个字符( karakters )组成的团队,我知道我可以通过外键找到字符,但是我想要 3 个字符而不仅仅是 1 个我想知道是否在我的表中 TeamI 需要放置 KarakterId1、KarakterId2、KarakterId3 ?

Or do you usually do this in another simpler way that I don't know of (Because I can not find it)还是您通常以我不知道的另一种更简单的方式执行此操作(因为我找不到它)

Thanks in advance!提前致谢!

That's a many-to-many relationship between teams and characters: where one team has several characters, and a given character may belong to more than one team.这是团队和角色之间的多对多关系:一个团队有多个角色,而一个给定的角色可能属于多个团队。

A normalized design implies creating a third table, called a bridge table, to represent that relationship, with one row for each team/character tuple, and foreign key constraints the reference the parent tables.规范化设计意味着创建第三个表,称为桥表,以表示该关系,每个团队/角色元组占一行,外键约束引用父表。

create table teams (
    team_id int primary key,
    user_id int not null,
    name varchar(50)
);

create table characters (
    character_id int primary key,
    name varchar(50)
);

create table team_characters (
    team_id int references teams(team_id),
    character_id int references characters(character_id),
    primary key (team_id, character_id)
);

Precisely verbalizing the constraint in natural language usually reveals the correct solution.用自然语言精确地表达约束通常会揭示正确的解决方案。 Here are two possible examples, there can be more.这里有两个可能的例子,可能还有更多。


  • Each karakter may be in more than one team;每个卡拉克手可以一个以上的团队中; for each team: more than one karakter may be in that team.每个团队:该团队中可能有不止一个卡拉克特。
karakter {K}
      PK {K}


team {T}
  PK {T}


team_karakter {T, K}
           PK {T, K}

          FK1 {K} REFERENCES karakter {K}
          FK2 {T} REFERENCES team     {T}

  • Each karakter may be in more than one team;每个卡拉克手可以一个以上的团队中; for each team: exactly three karakters are in that team.每队:恰好有三个karakters那支球队。
karakter {K}
      PK {K}


team {T, K1, K2, K3}
  PK {T}

 FK1 {K1} REFERENCES karakter {K}
 FK2 {K2} REFERENCES karakter {K}
 FK3 {K3} REFERENCES karakter {K}

 CHECK ((K1 <> K2) AND (K1 <> K3) AND (K2 <> K3))

Note:笔记:

All attributes (columns) NOT NULL

PK = Primary Key
FK = Foreign Key

暂无
暂无

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

相关问题 我想根据页面ID将数据从多个表中放入结帐页面 - I want to get data into my checkout page from multiple table according it's page id 在检查另一个表的最小和最大ID之后,我需要从表中找到丢失的ID - I need to find the missing Id's from the table after checking the min and max id's from another table 我需要从SQL的哪些联接中获取基于不同表中ID的类别名称? - What joins in SQL do I need to get the Category name based on the ID from a different table? 我需要从一个记录表中获取一个 ID 列表,该表的子记录都处于非活动状态 - I need to get a list of ID's from a record table whose child records all are inactive 如何将其他表格的主要ID插入新表格 - How do I insert primary id's from other tables into a new table 从其他表中获取 id 然后插入到表中。 我想点击按钮然后回答当前问题 - Get id from other table then insert into table. I want to click the button then answer current question 如何检查表的 ID 是否与 SQL 中另一个表的另一个 ID 匹配 - How can I check if the ID's of a table match another ID's of another table in SQL 具有其他表的多个id的SQL字段 - SQL field with multiple id's of other table 需要来自同一表上不同ID的信息Laravel - Need Info from different id on the same table Laravel 如何通过将相同的值添加到其他表中的 ID 来插入多行,从而创建新的对? - How can I INSERT multiple rows by adding same value to ID's from other table thus creating new pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM