Click here to Skip to main content
15,896,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making desktop appication using c#.Net and Sql Server.I have taken a form in which there are 4 labels prospect id ,customer id,customer name and services and same are the fields of database.Nw i want that prospect id and customer id both should not have duplicate values.so please tell me the solution for this

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.





regards
Posted
Updated 5-May-11 20:37pm
v2
Comments
yesotaso 6-May-11 9:36am    
The reference table "conversion" does not have "Customer_ID" as Primary Key simple as that. If you want to add such constraint, "references conversion(Prospect_ID, Customer_ID)" but of course you do not have Prospect_ID column "Customer_follow_up" table and Customer_ID itself doesnot guarantee that you get right "conversion" record. It is design fault. 1st solution I can think of is make only "Customer_ID" as primary key and put a unique constraint on "Prospect_ID ,Customer_ID". that way it will work BUT I do not know the intention of making a PK with 2 fileds in the 1st place this may hurt your design.

You can have only one Primary Key[^] in a table.

But you can have multiple Unique Key[^].
 
Share this answer
 
Comments
Peter_in_2780 5-May-11 8:48am    
Didn't we go round this circus a day or two ago? I'm getting bored with this question...
First of all you can not create multiple primary key, you can create single primary key on more than one column.

CREATE TABLE dbo.test
(
test1 int,
test2 int,
test3 int,
CONSTRAINT somename PRIMARY KEY (test1 , test2 )

)
 
Share this answer
 
Comments
yesotaso 5-May-11 10:12am    
Can Mr/Ms downrater? Can you elaborate? The statement above is perfecly fine though seems repetition but code was not given before to clear confusion.
shivani 2013 6-May-11 2:37am    
i hve updated question see it nw
shivani 2013 6-May-11 2:37am    
i hve updated question see it
try this Link

Composite Key[^]
 
Share this answer
 
v2
Comments
Peter_in_2780 5-May-11 8:46am    
As Toniyo says, there can only be one PRIMARY key in a table.
Its better keep a primary key as separate and use Unique Key combination on those two DB fields. Refer the concept called as Composite primari key !

Thanks !.
 
Share this answer
 
Comments
shivani 2013 6-May-11 2:27am    
can u pls explain it little bit more with eg
You can only have one primary key, but you can have multiple columns in your primary key.
You can also have Unique Indexes on your table, which will work a bit like a primary key in that they will enforce unique values, and will speed up querying of those values.
 
Share this answer
 
Comments
shivani 2013 6-May-11 2:27am    
can u pls explain it little bit more with eg

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