sql - Cannot create Foreign Key Constraint -
i have simple address book collects people , companies info. have following 3 tables , others email, phonenumber, address linked contact:
create table [dbo].[contact] ( [id] int identity (1, 1) not null, ... constraint [pk_contatto] primary key clustered ([id] asc) ); create table [dbo].[person] ( [id] int identity (1, 1) not null, [idcontact] int not null, [idcompany] int null, ... constraint [pk_person] primary key clustered ([id] asc), constraint [fk_person_company] foreign key ([idcompany]) references [dbo].[company] ([id]) on delete set null on update cascade, constraint [fk_person_contact] foreign key ([idcontact]) references [dbo].[contact] ([id]) on delete cascade on update cascade ); create table [dbo].[company] ( [id] int identity (1, 1) not null, [idcontact] int not null, ... constraint [pk_company] primary key clustered ([id] asc), constraint [fk_company_contact] foreign key ([idcontact]) references [dbo].[contact] ([id]) on delete cascade on update cascade );
basically want if deletes contact person/company associated deleted. also, if deletes company associated person, idcompany of person must set null.
the constraint cannot created due "loop creation or additional propagation path creation" fk_person_contact.
what missing?
i suspect 'loop creation' because person table references contact table , company table , company table references contact table.
can person record , company record reference different contact records?
person -> contact -> company -> contact b
or should person record , company record reference same contact?
person -> contact -> company -> contact a
how relationship between contact, person , company defined?
- 1 contact has many persons?
- 1 contact has many companies?
- 1 person has many contacts?
- 1 person has 1 company?
- 1 person has many companies?
- 1 company has many contacts?
- 1 company has many persons?
it may need think 1 or more link tables model relationship properly.
Comments
Post a Comment