Lets say you have the following entities in your database:
Image http://lh6.ggpht.com/_UpGtM3B8h1U/Sa7SqfWmgDI/AAAAAAAAAHE/epqtm7EnoFg/test.gif
You want to be able to add a note to all three of the other entity tables (User, Client and Project). How would you set this up?
- Add a tableName and a FKId to the Note table?
- Create a separate table for each associated Note group (ie., ClientNote, UserNote)
- Create a separate field in Note for each Foreign key (UserId, ClientId, etc)
I seem to be going back and forth between option 2 and 3, although I’d be up for 1 if there were an elegant implementation. Option 2 is attractive because there are no nulls. Option 3 is attractive because a note feels like a note no matter what it is associated with.
Any thoughts?
I would typically go with option 2. I don’t stress about the number of tables in a database within reason.
The big question is, are these notes going to be handled as a single item? For example, are you going to ever say, ‘Show me all notes, regardless of whether it’s for a client or user or project.’ I can’t think of any situations where that would be useful. You’re going to typically say, ‘Show me THIS CLIENT’S notes’ or ‘Show me THIS PROJECT’S notes.’
Since they are not logically one item I suggest not trying to model them physically as one item.