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
Post a Comment