The right way to structure the "notes" table for multiple datasets?

Please excuse the title, I had problems with the wording of my question, not being overly descriptive.

My application has tables that like it:
contacts
Properties
<Strong> events

I am adding a method to attach notes to elements in the above tables. I would like one note to be associated with a contact, property or event (or a combination of three).

Currently, the notes table is as follows:

noteID int
noteCreated datatime
noteContent text
userID int (userid that created the note)
contactID int
propertyID int
eventID int

The part in question is in bold. Right now, when I create a note for an event , I just insert a note and also set eventID . If the event also refers to a contact , I can add contactID (contactID and eventID will be set). Although it works, I believe that it is inefficient and not normalized.

, , " ", , "" . , .

, , , targetType, . , . (notes_properties, notes_contacts ..).

noteID int
targetID int
targetType int

. :)

+3
3

, 1 1, , , noteID , .

" ", , , , contact_notes - contactID noteID.

, . , .

+3

, ​​:

noteID
noteCreated
noteContent 
userID
noteType (contact, property or event)
RelatedID

, :

FROM Contacts C
INNER JOIN Notes N on C.ID = N.RelatedID and N.noteType = 'contact'

FROM Property P
INNER JOIN Notes N on P.ID = N.RelatedID and N.noteType = 'property'
0

...

, , ( ).

... " " ( , , ).

, :

noteID int
targetID int
targetType int

, . , ( , , ) .

, :

ID int          'autonumber to provide a reliable and efficient unique key'
noteID int
contactID int   'allow nulls'
propertyID int  'allow nulls'
eventID int     'allow nulls'
0

Source: https://habr.com/ru/post/1789863/


All Articles