我有一个棒球数据库。我收到错误错误没有唯一约束匹配给定引用表投手的键。
这是架构
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)
);
在我查找的其他主题中,建议添加唯一标识符,但在此模式中,可以使用相同的名称并在同一个团队中。我该如何解决这个问题?
答案 0 :(得分:2)
行
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),
{...}
)
几乎可以肯定是导致你悲伤的那些人。您在每种情况下都尝试引用引用表中的非唯一列,但这只是不起作用。
好消息是,您不需要这些references
条款,因为它们由表级FOREIGN KEY
条款处理,每个条款引用一个<由于是复合主键, 连接值 的列的em> group 是唯一的。