Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Database design question

Author  Topic 

alanlambert
Starting Member

26 Posts

Posted - 2010-01-12 : 15:57:17
I've got a couple of tables that store contacts and their relationships for storing contacts. Simplified versions are as follows:
Contacts
========
ContactId int (PK)
ContactName nvarchar(50)

Relationships
=============
RelationShipId int (PK)
ContactId1 int
ContactId2 int
RelationshipTypeId int
There are foreign key relationships between ContactId and ContactId1, and ContactId and ContactId2

I need to be able to mark one relationship for each user as the default and contacts sharing a relationship may have different default relationships.

e.g.
ContactId ContactName
1 Person A
2 Person B
3 Person C
4 Person D

RelationshipId ContactId1 ContactId2 RelationshipTypeId
1 1 2 1
2 1 3 2
3 2 3 1
4 2 4 2
5 4 1 1
6 1 2 2
Defaults may then be:
ContactId DefaultRelationshipId
1 2
2 1
3 3
4 5
My question is how should I record this default relationship in the database? Given it's to do with a Relationship the obvious place is the Relationships table but the problem here is that a Contact can exist on either side of a Relationship and the default must be across both sides. I could add Default1 and Default2 columns but this becomes quite messy to handle with setting and clearing the defaults.

I could add a DefaultRelationshipId to the Contact table but this would allow me to set relationship that might have nothing to do with the Contact (not a good option, I would have thought).

Any advice as to the best approach?

Many thanks

Alan

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-01-12 : 16:46:49
One idea would be to have your relationship table like:

ContactID
RelatedContactID
RelationshipTypeID

So if person A and B were Mother-Daughter they would be in there twice instead of once:

A B Daughter
B A Mother


The advantage to that model would be that the subject person is always ContactID. Then you could treat the model like a typical hierarchical structure similar to [ID] [ParentID]. And you won't need any code that conditionally uses contactID1 or contactID2 depending which side of the relationship the subject contact is.

Setting the default will likely require a check constraint somewhere. If you add it to the contact table then you need to make sure the relationship.contactID is the contact.contactid. If you add the default flag to Relationship you need to make sure only one row per contactid is set to true. I tend towards putting it in Contact.

Be One with the Optimizer
TG
Go to Top of Page

alanlambert
Starting Member

26 Posts

Posted - 2010-01-13 : 03:36:34
TG

I like your idea. Unfortunately I'm not designing the database. I've just been asked to add the 'default' option to a database already in production and I don't think I'm going to be allowed to refactor it like this.

I will bear this in mind for the future, however.

Many thanks for your advice.

Alan
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-01-13 : 10:21:39
If that is the case then you will need to put the default in Contact. Otherwise, as you state, a one column flag in [Relationship] would not be sufficient. At best it could be ambiguous (which ID does it apply) and at worst impossible (when both ID2 and ID2 have the same default relationship). Plus it just makes better sense in [Contact] because every contact can only have (at most) one default.

Be One with the Optimizer
TG
Go to Top of Page

alanlambert
Starting Member

26 Posts

Posted - 2010-01-13 : 15:49:10
TG

Again, thank you for your help.

Alan
Go to Top of Page
   

- Advertisement -