Please excuse the title, I had trouble wording my question without being overly descriptive.
My application has tables likes these:
contacts
properties
events
I am adding a method of attaching notes to the items in the above tables. I would like it so that a single note can associate with a contact, property or event (or a combination of the three).
Currently my notes table looks like this:
noteID int
noteCreated datatime
noteContent text
userID int (userid that created the note)
contactID int
propertyID int
eventID int
The portion in question is in bold. Right now, when I create a note for an event, I simply insert the note and also set the eventID. If the event also relates to a contact, I can add the contactID as well (contactID and eventID would be set). While it works, I think it is inefficient and not properly normalized.
What I am trying to do is create a one-to-many relationship, problem is that the “many” part can have different target tables. At the same time I want to reduce the number of queries necessary to select or insert a note.
My thought was to create a table that connects them together, then give properties, contacts, and events their own unique targetType that stays constant. But I still feel that is not the best way to do it. Alternatively I can create a separate relationship table for each target table (notes_properties, notes_contacts, etc…).
noteID int
targetID int
targetType int
Much help would be appreciated. Thank you 🙂
If the relationship between notes and contacts, properties & events is 1 to 1, e.g., a contact can have only one note, then the best way to model it would be to simply add a
noteIDcolumn to the contact, properties and events table.If it’s a many-to-many relationship, e.g., a contact can have many notes, then you would want to create a separate table, say
contact_noteswith two columns –contactIDandnoteID.Don’t get hung up on the fact that a note can be associated to multiple entities. It really doesn’t affect the way you model it at all.