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


classcode table

create table [dbo].[classcode]( [schoolcode] [nvarchar](10) not null, [classcode] [nvarchar](4) not null, [classname] [nvarchar](50) not null, [classrange] [int] not null, [classduration] numeric(38,2) not null, [userid] [nvarchar](30) null, [recorddate] [smalldatetime] null,    constraint [pk_classcode] primary key clustered  (     [schoolcode] asc,     [classcode] asc,     [classrange] )with (pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on) on [primary] ) on [primary] 

examdeclaration table

create table [dbo].[examdeclaration]( [schoolcode] [nvarchar](10) not null, [classcode] [nvarchar](4) not null, [examcode] [nvarchar](4) not null, [registationfess] numeric(38,2) not null, [registatinstartdate] [date] not null, [registatinenddate] [date] not null, [examstartdate] [date] not null, [userid] [nvarchar](30) null, [recorddate] [smalldatetime] null  constraint [pk_examdeclaration] primary key clustered  (     [schoolcode] asc,     [classcode] asc,     [examcode] asc )with (pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on) on [primary] ) on [primary]   go  alter table [dbo].[examdeclaration]  check add  constraint [fk_examdeclaration_classcode] foreign key([classcode]) references [dbo].[classcode] ([classcode]) go  alter table [dbo].[examdeclaration] check constraint [fk_examdeclaration_classcode] go 

i trying set foreign key classcode

but got error

msg 1776, level 16, state 0, line 2 there no primary or candidate keys in referenced table 'dbo.classcode' match referencing column list in foreign key 'fk_examdeclaration_classcode'. msg 1750, level 16, state 0, line 2 not create constraint. see previous errors. msg 4917, level 16, state 0, line 2 constraint 'fk_examdeclaration_classcode' not exist. msg 4916, level 16, state 0, line 2 not enable or disable constraint. see previous errors.

what problem in these 2 tables?

thank you..

your foreign key must match primary key! if declare primary key pk(x int,y int, z int), fk must declared this: fk (a int, b int, c int) references mytable(x,y,z).

in case, pk (schoolcode, classcode, classrange). means should declare fk this:

alter table [dbo].[examdeclaration] check add constraint [fk_examdeclaration_classcode] foreign key(schoolcode, classcode, classrange) references [dbo].[classcode] (schoolcode, classcode, classrange) go

modify table examdeclaration, add column 'classrange'.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -