sql - Alter statement Conflict with foreign key, but ID exisits in both table -


i've 2 tables, here i'm trying insert fk relationship. but, i'm getting errors when try alter table create fk relationship please help... have club id in both tables, commented below, not sure why i'm still getting constraint error.

what causing constraint error?

create table [dbo].[clubs]( [clubid] [int] identity(1,1) not null,  // clubid here [name] [nvarchar](128) not null, [description] [nvarchar](2047) null, [created] [datetime] not null, [modified] [datetime] null,   constraint [pk_clubs] primary key clustered  (     [clubid] 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  create table [dbo].[memberclubs]( [memberclubid] [int] identity(1,1) not null, [memberid] [bigint] not null, [clubid] [int] not null,              //club id here  constraint [pk_memberclubs] primary key clustered  (     [memberclubid] 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    set identity_insert [dbo].[memberclubs] on  go    insert [dbo].[memberclubs] ([memberclubid], [memberid], [clubid]) values (1, 1, 1) go    insert [dbo].[memberclubs] ([memberclubid], [memberid], [clubid]) values (2, 2, 1) go    insert [dbo].[memberclubs] ([memberclubid], [memberid], [clubid]) values (3, 3, 1) go    insert [dbo].[memberclubs] ([memberclubid], [memberid], [clubid]) values (4, 4, 1) go    insert [dbo].[memberclubs] ([memberclubid], [memberid], [clubid]) values (5, 5, 1) go    set identity_insert [dbo].[memberclubs] off go   // *** error on here  alter table [dbo].[memberclubs]  check add  constraint [fk_memberclubs_clubs] foreign key([clubid]) references [dbo].[clubs] ([clubid]) go  // $$$ second fk constraint error on here alter table [dbo].[memberclubs] check constraint [fk_memberclubs_clubs] go  alter table [dbo].[memberclubs]  check add  constraint [fk_memberclubs_members] foreign key([memberid]) references [dbo].[members] ([memberid]) go  alter table [dbo].[memberclubs] check constraint [fk_memberclubs_members] go 

the error:

msg 547, level 16, state 0, line * * * (above)

the alter table statement conflicted foreign key constraint "fk_memberclubs_clubs". conflict occurred in database "newdb", table "dbo.clubs", column 'clubid'. 

msg 4917, level 16, state 0, line $$$ (above)

constraint 'fk_memberclubs_clubs' not exist. 

msg 4916, level 16, state 0, line $$$ (above)

could not enable or disable constraint. see previous errors. 

you add 5 rows table memberclubs referencing club.clubid = 1, before adding foreign key relationship clubs.

so in order maintain referential integrity, you'll need add row clubid = 1 clubs before adding / enabling foreign key constraint:

set identity_insert [dbo].[clubs] on  insert [dbo].[clubs](clubid, name, description, created, modified)         values (1, 'foo', 'bar', current_timestamp, current_timestamp) set identity_insert [dbo].[clubs] off 

then foreign key creation succeeds:

alter table [dbo].[memberclubs]  check      add  constraint [fk_memberclubs_clubs] foreign key([clubid]) references [dbo].[clubs] ([clubid]) go 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -