简体   繁体   English

PSQL错误,没有唯一约束匹配给定表引用的键

[英]PSQL Error there is no unique constraint matching given keys for referenced table

I have a baseball database. 我有一个棒球数据库。 I'm getting the error Error there is no unique constraint matching given keys for referenced table pitcher. 我收到错误消息错误,没有给定引用表投手的键匹配的唯一约束。

Here's the schema 这是架构

CREATE TABLE Teams(
  Name varchar(30) NOT NULL Primary Key,
  Record varchar(10)
);

CREATE TABLE Player(
  Name varchar(30) NOT NULL,
  Num int NOT NULL,
  TeamName varchar(22) references Teams(Name),
  PRIMARY KEY(Name, Num, TeamName),
  Constraint NumCheck Check (Num < 100 and Num > -1)
);

CREATE TABLE Pitcher(
  PHanded varchar(10),
  PName varchar(30),
  PTeamName varchar(30),
  PNum int,
  PRIMARY KEY(PName, PTeamName, PNum),
  foreign key (PName, PTeamName, PNum) references Player(Name, TeamName, Num)
);



CREATE TABLE PosPlayer(
  PPHanded varchar(10),
  Position varchar(2),
  PPName varchar(30),
  PPNum int,
  BTeamName varchar(30),
  PRIMARY KEY(PPName, PPNum, BTeamName),
  foreign key (PPName, PPNum, BTeamName) references Player(Name, Num, TeamName)
);

CREATE TABLE Games(
  Id int Primary Key,
  SchedDate DATE,
  PlayedDate DATE,
  HomeName varchar(30),
  VisitName varchar(30),
  Winner varchar(30)
);


CREATE TABLE Pitches
(
  PID int,
  Outcome varchar(10),
  Bcount int,
  Scount int,
  Runners int,
  Type varchar(10),
  Speed int,
  Pitchnum int,
  PitName varchar(30) references Pitcher(PName),
  PitNum int references Pitcher(PNum),
  PitTeamName varchar(30) references Pitcher(PTeamName),
  BatName varchar(30) references PosPlayer(PPName),
  BatNum int references PosPlayer(PPNum),
  BatTeamName varchar(30) references PosPlayer(BTeamName),
  HomeTName varchar(30),
  VisitTName varchar(30),
  GId int references Games(Id),
  foreign key (PitName, PitNum, PitTeamName) references Pitcher (PName, PNum, PTeamName),
  foreign key (BatName, BatNum, BatTeamName) references PosPlayer (PPName, PPNum, BTeamName),
  Primary Key(PID, PitName, PitNum, PitTeamName, BatName, BatNum, BatTeamName, GID),
  Constraint Balls Check (Bcount > -1 and Bcount < 4),
  Constraint Scount Check (Scount > -1 and Scount < 3)
);

In the other threads I looked up it was suggested to add a unique identifier, but in this schema it is possible to have the same name and be on the same team. 在其他线程中,我建议添加一个唯一的标识符,但是在这种模式下,可以使用相同的名称并在同一团队中。 How do I fix this? 我该如何解决?

The lines 线

CREATE TABLE Pitches
(
{...}
  PitName varchar(30) references Pitcher(PName),
  PitNum int references Pitcher(PNum),
  PitTeamName varchar(30) references Pitcher(PTeamName),
  BatName varchar(30) references PosPlayer(PPName),
  BatNum int references PosPlayer(PPNum),
  BatTeamName varchar(30) references PosPlayer(BTeamName),
{...}
)

are almost certainly the ones causing you grief. 几乎可以肯定是引起您悲伤的原因。 You're trying in each case to reference a nonunique column in the referenced table, and that just won't work. 在每种情况下,您都试图在被引用的表中引用一个非唯一的列,但这是行不通的。

The good news is that you don't need these references clauses, as they're taken care of by the table-level FOREIGN KEY clauses, each of which references a group of columns whose concatenated values are unique, due to being a composite primary key. 好消息是您不需要这些references子句,因为它们是由表级FOREIGN KEY子句处理的,每个子句都引用一列的值,这些列的连接值是唯一的,因为它们是复合主键键。

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

相关问题 没有唯一约束匹配给定引用表的键 - No unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定键的引用表 - ERROR: there is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定键的引用表“bar” - ERROR: there is no unique constraint matching given keys for referenced table "bar" 远离错误:没有唯一约束匹配引用表的给定键 - Away around Error: There is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定表的键 - ERROR: No unique constraint matching given keys for referenced table 如何修复错误“没有唯一约束匹配引用表的给定键” - How to fix error "there is no unique constraint matching given keys for referenced table" "错误:没有唯一约束匹配引用表“事件”的给定键" - error: there is no unique constraint matching given keys for referenced table "incident" 无法解释此错误:没有与引用表的给定键匹配的唯一约束 - Unable to explain this error: there is no unique constraint matching given keys for referenced table Postgresql错误:没有唯一约束匹配给定键的引用表 - Postgresql ERROR: there is no unique constraint matching given keys for referenced table PostgreSQL错误:没有唯一约束匹配给定键的引用表 - PostgreSQL Error: there is not unique constraint matching given keys for referenced table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM