首页 > 解决方案 > 如何为一个表设置两个外键,并且这些外键引用另一个表中的相同主键?

问题描述

下面给出了子表的创建......

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,即团队表)中的相同主键列...

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

标签: databaserelational-database

解决方案


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

推荐阅读