Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Check Constraint Person Relations Expand / Collapse
Author
Message
Posted Thursday, October 22, 2009 1:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 22, 2009 8:06 PM
Points: 2, Visits: 2
Hi Guys. I have 2 tables. TPerson(PersonID, ...) and TPersonRelation(PersonRelationID, Person1ID (FK1), Person2ID(FK2), TypeOfRelationID). All fields are integers. The relations are the follow: PersonID --> Person1ID and the same PK Fields PersonID --> Person2ID. How can I implement both relations, if SQL Server don´t allow me to do 2 relations from the same field (PersonID). A trigger could solve this problem, but ... Do you know if exits a more better solution than the trigger?
I need to:
1)check Parent Child Relation, cascade deletion.
2)build an index with unique key using Person1ID and Person2ID(FK2).
Thanks in advance.. Luis
Post #806967
Posted Thursday, October 22, 2009 4:10 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:12 AM
Points: 1,322, Visits: 4,400
If I understand your requirements correctly, then you have your constraints the wrong way round. You say
How can I implement both relations, if SQL Server don´t allow me to do 2 relations from the same field (PersonID).
Indicating that you think the constraint should be from TPerson to TPersonRelation.

I assume you want to make sure that Person1Id and Person2ID in the TPersonRelation table both exists in the TPerson table, so the constraint is the other way round, and you actually want something like this.

ALTER TABLE TPersonRelation ADD CONSTRAINT FK_Person1ID FOREIGN KEY (Person1ID) REFERENCES TPerson(PersonID)

ALTER TABLE TPersonRelation ADD CONSTRAINT FK_Person2ID FOREIGN KEY (Person2ID) REFERENCES TPerson(PersonID)




Post #807019
Posted Thursday, October 22, 2009 3:53 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 22, 2009 8:06 PM
Points: 2, Visits: 2
Thanks for your reply.
All your assumptions are correctly.
Your suggest was my first approach to solve it, but ...
SQL Server Throw an Error:
Msg 547, Level 16, State 0, Line 2
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Person2ID". The conflict occurred in database "MYDATABASE", table "dbo.TPersons", column 'PersonID'.
Post #807486
Posted Thursday, October 22, 2009 4:06 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, May 10, 2012 9:49 AM
Points: 800, Visits: 1,759
Hi,
This error message indicates that you have values in tPersonRelation in Person2ID column that do not exist in tPerson. Here's an example:
create table tperson(personid int primary key, personname sysname)
go
create table tpersonrelation(person1 int, person2 int)
go
insert tperson (personid, personname) values(1, 'aaa')
go
--2 doesn't exist in tperson, but we can add this because there is no FK constraint yet
insert tpersonrelation(person1, person2) values(1, 2)
go
alter table tpersonrelation add constraint fk_person1 foreign key(person1) references tperson(personid)
go
--this statement fails because of the insert above
alter table tpersonrelation add constraint fk_person2 foreign key(person2) references tperson(personid)
go
drop table tpersonrelation
drop table tperson

Regards

Piotr


...and your only reply is slàinte mhath
Post #807491
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse