简体   繁体   English

错误引用的表中没有主键或候选键

[英]Error There are no primary or candidate keys in the referenced table

I'm getting this error when trying to create a table with foreign key: 尝试使用外键创建表时出现此错误:

There are no primary or candidate keys in the referenced table 'TeamToPlayers' that match the referencing column list in the foreign key 'FKey2'. 在引用表'TeamToPlayers'中没有与外键'FKey2'中的引用列列表匹配的主键或候选键。

I don't understand why, there is a primary key in the table TeamToPlayers . 我不明白为什么,表TeamToPlayers有一个主键。

Here are the queries: 以下是查询:

create table TeamToPlayers
(TeamName varchar(50) NOT NULL,
  PlayerName varchar(50) NOT NULL,
  primary key(TeamName,PlayerName),
  CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName)
)

create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES TeamToPlayers(PlayerName)
);

Table TeamToPlayers primary key consists of two fields - you must reference both as otherwise it's not a key. TeamToPlayers主键包含两个字段-您必须同时引用这两个字段,否则它不是键。 I think you may have your key the wrong way round - it should be on TeamToPlayers and referencing Players like so: 我认为您可能会以错误的方式获得密钥-应该在TeamToPlayers并像这样引用Players

create table TeamToPlayers
(   
    TeamName varchar(50) NOT NULL,
    PlayerName varchar(50) NOT NULL,
    primary key(TeamName,PlayerName),
    CONSTRAINT FKey FOREIGN KEY (TeamName) REFERENCES Teams(TeamName),
    CONSTRAINT FKey2 FOREIGN KEY (PlayerName) REFERENCES Players(PlayerName)
)

create table Players
(PlayerName varchar(50) NOT NULL,
primary key(PlayerName),
);

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

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