[sql] There are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key

In SQL Server , I got this error ->

"There are no primary or candidate keys in the referenced table 'BookTitle' that match the referencing column list in the foreign key 'FK_BookCopy_Title__2F10007B'."

I first created a relation called the BookTitle relation.

CREATE TABLE BookTitle (
ISBN            CHAR(17)       NOT NULL,
Title           VARCHAR(100)   NOT NULL,
Author_Name     VARCHAR(30)    NOT NULL,
Publisher       VARCHAR(30)    NOT NULL,
Genre           VARCHAR(20)    NOT NULL,
Language        CHAR(3)        NOT NULL,    
PRIMARY KEY (ISBN, Title))

Then I created a relation called the BookCopy relation. This relation needs to reference to the BookTitle relation's primary key, Title.

CREATE TABLE BookCopy (
CopyNumber         CHAR(10)            NOT NULL,
Title              VARCHAR(100)        NOT NULL,
Date_Purchased     DATE                NOT NULL,
Amount             DECIMAL(5, 2)       NOT NULL,
PRIMARY KEY (CopyNumber),
FOREIGN KEY (Title) REFERENCES BookTitle(Title))

But I can't create the BookCopy relation because the error stated above appeared.

I really appreciate some useful help.

This question is related to sql sql-server key candidate

The answer is


You need either

  • A unique index on Title in BookTitle
  • An ISBN column in BookCopy and the FK is on both columns

A foreign key needs to uniquely identify the parent row: you currently have no way to do that because Title is not unique.


Another thing is - if your keys are very complicated sometimes you need to replace the places of the fields and it helps :

if this dosent work:

foreign key (ISBN, Title) references BookTitle (ISBN, Title)

Then this might work (not for this specific example but in general) :

foreign key (Title,ISBN) references BookTitle (Title,ISBN)


BookTitle have a Composite key. so if the key of BookTitle is referenced as a foreign key you have to bring the complete composite key.

So to resolve the problem you need to add the complete composite key in the BookCopy. So add ISBN column as well. and they at the end.

foreign key (ISBN, Title) references BookTitle (ISBN, Title)

Examples related to sql

Passing multiple values for same variable in stored procedure SQL permissions for roles Generic XSLT Search and Replace template Access And/Or exclusions Pyspark: Filter dataframe based on multiple conditions Subtracting 1 day from a timestamp date PYODBC--Data source name not found and no default driver specified select rows in sql with latest date for each ID repeated multiple times ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database

Examples related to sql-server

Passing multiple values for same variable in stored procedure SQL permissions for roles Count the Number of Tables in a SQL Server Database Visual Studio 2017 does not have Business Intelligence Integration Services/Projects ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database How to create temp table using Create statement in SQL Server? SQL Query Where Date = Today Minus 7 Days How do I pass a list as a parameter in a stored procedure? SQL Server date format yyyymmdd

Examples related to key

How do I check if a Key is pressed on C++ Map<String, String>, how to print both the "key string" and "value string" together Python: create dictionary using dict() with integer keys? SSH Key: “Permissions 0644 for 'id_rsa.pub' are too open.” on mac SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch How to get the stream key for twitch.tv How to get key names from JSON using jq How to add multiple values to a dictionary key in python? Initializing a dictionary in python with a key value and no corresponding values How can I sort a std::map first by value, then by key?

Examples related to candidate

There are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key