views:

250

answers:

2

I'm attempting to create a contacts application that has two main entities - person and company. A person can have many emails, numbers, and addresses. A company can also have many emails, numbers, and addresses. I'm trying to determine the proper design for this scenario.

Option #1 - multiple foreign keys
Emails, numbers, and addresses will have two columns called person_id and company_id. Depending on which entity the data belongs to, one will be null and the other will contain an id linking back to the parent.

Option #2 - one table per type per entity
I duplicate each table so there would be a company_addresses table and a person_addresses table. I would have twice as many tables, but this is the solution that makes the most sense right now.

Option #3 - one link table
I create one table - "link". This table will contain four columns: source_id, source_entity, dest_id, dest_entity. So if a company gets a new number you would have a row like: 1, number, 2, company.

Option #4 - multiple link tables
I create a table for each type of link (company_address, person_address, company_email, person_email, etc.)

Which option would you choose?

A: 

Either Option #2 or I would do this:

You could create an EntityTable that will define an Id and Entity type, then you can have your Address, Email tables link to that one.

You then Create Person & Company Table that reference back to Entity. Otherwords your subclassing Entity.

Option #1 - I've used this in the past and it can become a headache to manage as things get complicated and grow.

Option #3 - You can't use referenctial integrity, and the cost you get from saving a few key strokes to do option #2 won't be worth cleaning up the bad data, or dealing with the extra complexity.

JoshBerke
+5  A: 

You've touched on a couple of practices I would argue that you avoid. I wrote more about this in Database Development Mistakes Made by AppDevelopers (eg exclusive arcs).

As to your problem, I would actually choose none of those options. What you're stumbling towards is a generic Party model. Basically you have a Party entity with subtypes such as Person and Organisation. Contacts has a Party ID as a foreign key. The use of a common superclass/superentity is much more profound than that however as you will find that you use this for many other things as well (eg the whole role concept).

A lot of these database design modelling problems have mature solutions to them but it doesn't tend to be the sort of thing programmers are ever taught. I highly recommend getting the book The Data Model Resource Book, Vol. 1: A Library of Universal Data Models for All Enterprises, which goes into much more detail about how to model people and organisations as well as many other typical problems.

The key point to remember here is that what you're doing has been done before.

cletus
Thanks cletus - I figured it's something that has been done before, and given the simplicity of the example I imagined there was a design practice around this. I'll check out the link and book.
Amit
Beat me to it, exactly what I was going to suggest but written a bit better I suspect. I'll certainly be looking into the links.
MadMurf