There are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key
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.
At the moment, the only unique index you have is a compound one on ISBN, Title
which is your primary key.
There are a number of options open to you, depending on exactly what BookTitle holds and the relationship of the data within it.
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.
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.
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.
There are no Primary or Candidate Keys in the referenced table
If you want to create a foreign key, it must reference either the primary key, or a field with a unique constraint.
If you want to display the customer's name, make the foreign key reference the CustomerID, and display the results with a join.
There are no primary or candidate keys in the referenced table ' ' that match the referencing column list in the foreign key
Each value of a Foreign Key constraint needs to reference exactly one row of the target table, and the DBMS needs to be able to guarantee this based on the table definitions.
Although the ID_contrat column is defined as auto-incremented, it is not constrained to be unique, because you've defined a multi-column Primary Key on the Contrat table. So the DBMS is saying that a single ID_contrat in your proposed Foreign Key could potentially match multiple rows in the Contrat table, which is not allowed. (This seems surprising, because you probably know that it will only match one, but the DBMS doesn't!)
You need to do one of two things:
- Define your foreign key across multiple columns, so that it matches the multiple columns guaranteed to be unique on the other table; that will require you to have all three columns in the FonctionContrat table (ID_contrat, ID_client, and code_contrat)
- Adjust the definition of the Contrat table to say that ID_contrat is in fact unique across all rows, either by changing the Primary Key definition, or adding an additional Unique Constraint on the ID_contrat column
I suspect the second option is the appropriate one here: it's rather unusual to use an auto-increment column and then insert duplicates into it.
What you possibly intended was two different unique constraints / indexes: one on ID_contrat and a separate one on combinations of ID_client and code_contrat. Only one of those can be marked as the Primary Key, but the other can be a Unique Index, which has most of the same functionality in practice; either one can be marked "clustered", which tells SQL Server to physically lay out the table based on that column.
No Primary or candidate keys in the referenced table that match column list in foreign key
As table CART
has composite primary key, then you should to reference to a composite primary key:
create table ORDERS
(
Order_ID int NOT NULL primary key,
Total_Price float NOT NULL,
Payment_Method varchar(30) NOT NULL,
User_ID int NOT NULL,
CART_ID int NOT NULL,
foreign key (User_ID)
references USERS(User_ID),
foreign key (Cart_ID, User_ID)
references CART(Cart_ID, User_ID),
)
There are no primary or candidate keys in the referenced table 'Card' that match the referencing column list in the foreign key
The PK in Card
is (GiveDate, PN)
, but your FK references a key (PN, GiveDate)
- the order of the columns must match! So try this in your Registration
table:
Create table [Registration]
(
EntryDate Date not null,
ExitDate Date,
RoomId int not null,
CardGiveDate date not null,
PN nvarchar(50) not null,
PRIMARY KEY (PN, EntryDate, CardGiveDate),
-- make sure to specify the columns in the same order as they are defined in the referenced table!
FOREIGN KEY (PN, CardGiveDate) REFERENCES [Card](GiveDate, PN)
)
SQL Server : there are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key 'FK'
Your table Autocare
has a compound primary key made up from two columns:
PRIMARY KEY CLUSTERED ([IDAutocar] ASC, [IDTipAutocar] ASC),
Therefore, any table that wishes to reference Autocare
must also provide both columns in their foreign key!
So this will obviously not work:
CONSTRAINT [FK_Curse_Autocare]
FOREIGN KEY ([IDAutocar])
REFERENCES [Autocare]([IDAutocar])
since it references only one of the two columns of the primary key of Autocare
.
You need to add that second column IDTipAutocar
to your Curse
table and include it in your foreign key:
CONSTRAINT [FK_Curse_Autocare]
FOREIGN KEY ([IDAutocar], [IDTipAutocar])
REFERENCES [Autocare]([IDAutocar], [IDTipAutocar])
Your foreign keys must always reference the WHOLE primary key - not just parts of it.
There are no primary or candidate keys in the referenced table ... that match the referencing column list in the foreign key
For [dbo].[WeibullFilterDetails]
you defined the primary key as ([ProjectTeamId],[WeibullFilterDetailsId])
, yet in your REFERENCES
clause you wrote ([WeibullFilterDetailsId],[ProjectTeamId])
-- the order doesn't match. Try:
CREATE TABLE [dbo].[WeibullSummaryDetails](
...
FOREIGN KEY ([ProjectTeamId],[WeibullFilterDetailsId])
REFERENCES [dbo].[WeibullFilterDetails]([ProjectTeamId],[WeibullFilterDetailsId])
);
Related Topics
Declare Variable in Sqlite and Use It
How to Delete Duplicate Rows Without Unique Identifier
How to Create Composite Primary Key in SQL Server 2008
Use MySQL Spatial Extensions to Select Points Inside Circle
SQL Performance on Left Outer Join VS Not Exists
SQL Server Check Case-Sensitivity
SQL Server - in Clause With a Declared Variable
How to Get the Difference in Years from Two Different Dates
Parse Comma-Separated String to Make in List of Strings in the Where Clause
Pass Multiple Values in Single Parameter
Sqlite - How to Join Tables from Different Databases
Select/Group by - Segments of Time (10 Seconds, 30 Seconds, etc)
Oracle: What Does '(+)' Do in a Where Clause
How to Specify Condition in Count()
SQL Server:Transpose Rows to Columns
SQL Server Indexes - Ascending or Descending, What Difference Does It Make