简体   繁体   English

没有唯一约束与给定键匹配的具有多个主键的引用表

[英]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约束要求在引用表的(一组)列上具有任何 UNIQUEPRIMARY 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.

相关问题 没有唯一约束匹配给定引用表的键 - No unique constraint matching given keys for referenced table sqlalchemy模式:没有唯一约束匹配给定表的键 - sqlalchemy schema: there is no unique constraint matching given keys for referenced table PSQL错误,没有唯一约束匹配给定表引用的键 - PSQL Error there is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定键的引用表 - ERROR: there is no unique constraint matching given keys for referenced table 没有与引用表“用户”的给定键匹配的唯一约束(PostgreSQL) - No unique constraint matching given keys for referenced table “users” (PostgreSQL) 引用表“exam_subjects”的给定键没有唯一的约束匹配 - There is no unique constraint matching given keys for referenced table “ exam_subjects” 错误:没有唯一约束匹配给定键的引用表“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 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