Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created two tables one table having one primary key with two columns

create table PSM.dbo.conversion(Prospect_ID int,Customer_ID int ,Customer_Name varchar(50),Customer_Products_Services varchar(50),constraint pk1 primary key(Prospect_ID ,Customer_ID  ));


it run successfully


2nd table as foreign key of one column only

create table PSM.dbo.Customer_follow_up(Customer_ID int references conversion(Customer_ID),Customer_Name varchar(50),Customer_Products_Services varchar(50),Feedback nvarchar(1000));


here,getting an error


Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'PSM.dbo.conversion' that match the referencing column list in the foreign key 'FK__Customer___Custo__5AEE82B9'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
Posted

When you add a foreign that does not exist as a primary key in the referenced table you will get this error.
Maybe this[^] can explain this better.
 
Share this answer
 
If Customer_ID will be a unique field then you can try this...I have put the UNIQUE constraint with Customer_ID field and FK will be created successfully....

SQL
create table dbo.conversion(Prospect_ID int,Customer_ID int UNIQUE ,Customer_Name varchar(50),Customer_Products_Services varchar(50),constraint pk1 primary key(Prospect_ID ,Customer_ID ));

create table dbo.Customer_follow_up(Customer_ID int references conversion(Customer_ID),Customer_Name varchar(50),
Customer_Products_Services varchar(50),Feedback nvarchar(1000));


Otherwise the reason mentioned in the Solution 1 will define it in detail..

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900