[英]No unique constraint matching given keys for referenced table with multiple primary keys
I am trying to link these two tables but am receiving the error: 我正在尝试链接这两个表,但收到错误:
There is no unique constraint matching given keys for referenced table "accomplices". 没有与引用表“帮凶”的给定键匹配的唯一约束。
Note Robberies
is another table. 注意Robberies
是另一张桌子。
I used this to create the Accomplices table (This is when the error occurs): 我用它来创建“共犯”表(这是发生错误的时间):
CREATE TABLE info.Accomplices (
RobberID INTEGER,
BankName VARCHAR,
City VARCHAR,
RobberyDate DATE,
Share DECIMAL NOT NULL,
PRIMARY KEY(RobberID, BankName, City, RobberyDate),
FOREIGN KEY(BankName, City, RobberyDate)
REFERENCES info.Robberies(BankName, City, RobberyDate)
);
And this to create the Robbers table: 并以此创建强盗表:
CREATE TABLE info.Robbers (
RobberID INTEGER,
Nickname VARCHAR,
Age INTEGER,
NoYears INTEGER,
PRIMARY KEY(RobberID),
FOREIGN KEY(RobberID) REFERENCES info.Accomplices(RobberID),
CONSTRAINT AgeCheck CHECK (Age > NoYears)
);
Does the foreign key in the Robbers
table need to match all components that make up the primary key in the Accomplices
table? Robbers
表中的外键是否需要匹配构成Accomplices
表中的主键的所有组件?
Does the foreign key in the Robbers table need to match all components that make up the primary key in the Accomplices table? 强盗表中的外键是否需要匹配构成共犯表中的主键的所有组件?
Not exactly. 不完全是。 It does not have to be the PK. 它不一定是PK。 A FK constraint requires any UNIQUE
or PRIMARY KEY
constraint on the (set of) column(s) in the referenced table. FK约束要求在引用表的(一组)列上具有任何 UNIQUE
或PRIMARY KEY
约束。 If it's not unique it cannot be referenced by FK. 如果不是唯一的,则FK无法引用它。 Theoretically you could add a UNIQUE
constraint to accomplices
: 从理论上讲,您可以向accomplices
添加UNIQUE
约束:
CREATE TABLE info.Accomplices (
robberid integer,
bankname varchar,
city varchar,
robberydate date,
share decimal not null,
PRIMARY KEY(robberid, bankname, city, robberydate),
UNIQUE(robberid),
FOREIGN KEY ...
);
.. which makes remarkably little sense from a design perspective, but goes to show the requirements for the given FK constraint in robbers
. 从设计的角度来看,这没有什么意义,但是它显示了robbers
给定FK约束的要求。
I suspect there is a logical problem with your database design. 我怀疑您的数据库设计存在逻辑问题。
Related: 有关:
CREATE TABLE info.Robberies (
BankName VARCHAR,
City VARCHAR,
RobberyDate DATE,
Amount DECIMAL NOT NULL,
PRIMARY KEY(BankName, City, RobberyDate),
FOREIGN KEY(BankName, City) REFERENCES info.Banks(BankName, City)
);
CREATE TABLE info.Robbers (
RobberID INTEGER,
Nickname VARCHAR,
Age INTEGER,
NoYears INTEGER,
PRIMARY KEY(RobberID),
--FOREIGN KEY(RobberID) REFERENCES info.Accomplices(RobberID),
CONSTRAINT AgeCheck CHECK (Age > NoYears)
);
CREATE TABLE info.Accomplices (
RobberID INTEGER,
BankName VARCHAR,
City VARCHAR,
RobberyDate DATE,
Share DECIMAL NOT NULL,
PRIMARY KEY(RobberID, BankName, City, RobberyDate),
FOREIGN KEY(RobberID) references info.Robbers(RobberID),
FOREIGN KEY(BankName, City, RobberyDate) REFERENCES
info.Robberies(BankName, City, RobberyDate)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.