sql server - There are no primary or candidate keys in the referenced table -


i have 2 tables:

table1


c_id float - pk
field1
field2
field3
p_id [uniqueidentifier]

and

table2


p_id nvarchar
s_id float

where both p_id , s_id part of primary key.

i tried create foreign key on table1:

alter table table1 add constraint fk_table1_table2 foreign key (p_id)      references table2(p_id)  

and got error:

there no primary or candidate keys in referenced table 'dbo.table2' match referencing column list in foreign key 'fk_table1_table2'.

am getting error because data type of p_id uniqueidentifier , in table2 p_id nvarchar? there workaround?

you have use fk includes same columns in pk

create table parenttest ( somenumber int, name varchar(25), primary key (somenumber, name) )   create table childtest ( somevalue varchar(25), somenumber int, name varchar(25), foreign key (somenumber, name) references parenttest (somenumber, name) ) 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -