【问题标题】:How to set two foreign key for one table and these foreign keys are referencing same primary key in another table?如何为一个表设置两个外键,并且这些外键引用另一个表中的相同主键?
【发布时间】:2018-05-21 18:48:05
【问题描述】:

子表的创建如下.....

create table match_detail (
  match_id int primary key auto_increment,
  team1_id int(5),
  team2_id int(5), 
  foreign key (team1_id) references team(team_id),
  foreign key(team2_id) references team(team_id)
);

父表数据如下......

create table team(
  team_id int(5) primary key,
  team_name varchar(20)
);

我想在子表(team1_id,team2_id)中创建两个外键,这两个字段引用另一个表(team_id,即团队表)中的相同主键列...

你能帮我解决这个问题吗?

【问题讨论】:

  • 您正在为密钥(team2_id)引用团队(team1_id),我猜是复制粘贴问题
  • 你能帮忙解决这个问题吗????

标签: database relational-database


【解决方案1】:

您的 SQL 不正确。

create table match_detail (match_id int primary key auto_increment,
team1_id int(5),
team2_id int(5), 
foreign key (team1_id) references team(team_id),
foreign key (team2_id) references team(team_id))

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2020-07-27
    • 1970-01-01
    • 2012-07-02
    • 2017-01-16
    • 2019-12-23
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    相关资源
    最近更新 更多