且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在引用表中没有与外键中的引用列列表匹配的主键或候选键

更新时间:2023-02-02 23:24:58

外键通过将列连接到另一个表中的唯一键来工作,并且该唯一键必须定义为某种形式的唯一索引,因为它是主索引键或其他唯一索引.

Foreign keys work by joining a column to a unique key in another table, and that unique key must be defined as some form of unique index, be it the primary key, or some other unique index.

目前,您唯一的唯一索引是ISBN, Title上的复合索引,这是您的主键.

At the moment, the only unique index you have is a compound one on ISBN, Title which is your primary key.

有很多选项可供您选择,具体取决于BookTitle拥有什么以及其中的数据关系.

There are a number of options open to you, depending on exactly what BookTitle holds and the relationship of the data within it.

我会猜测,书名中的每一行ISBN都是唯一的.假设是这种情况,然后将主键更改为仅在ISBN上,并更改BookCopy,以便您拥有ISBN并加入ISBN,而不是Title.

I would hazard a guess that the ISBN is unique for each row in BookTitle. ON the assumption this is the case, then change your primary key to be only on ISBN, and change BookCopy so that instead of Title you have ISBN and join on that.

如果需要将主键保持为ISBN, Title,则需要将ISBN存储在BookCopy中,并将Title和外键存储在两列中,或者需要在BookTitle(Title)上创建唯一索引)作为不同的索引.

If you need to keep your primary key as ISBN, Title then you either need to store the ISBN in BookCopy as well as the Title, and foreign key on both columns, OR you need to create a unique index on BookTitle(Title) as a distinct index.

通常,您需要确保REFERENCES子句中的一个或多个列与父表中的唯一索引完全匹配:在您的情况下,它会失败,因为您在Title一个人.

More generally, you need to make sure that the column or columns you have in your REFERENCES clause match exactly a unique index in the parent table: in your case it fails because you do not have a single unique index on Title alone.