One table versus multiple tables

I have the following tables: - messages - files - Events - documents

Each message, file, event, document may have comments.

What is the best database schema for this, and why ?

First decision

  • comment table (comment_id, author_id, comment)
  • 4 tables for creating relationships (posts_comments (post_id, comment_id), files_comments (file_id, comment_id), events_comments (event_id, comment_id), documents_comments (document_id, comment_id))

Second solution

  • comment table (comment_id, author_id, comment)
  • items_comments (comment_id, parent_type (enum ['post', 'file', 'event', 'document']), parent_id)

What is the best solution for this or which of the two should I use?

+3
3

/ . , . , ( FTS , ).

, , ( ) .

(, , , ). RI . , , .

 select * from documents d left join doc_comments c 
                           on d.id = c.docid 
                           where d.id=42;

, .

. OP "", " " (, ). , , ... ... , , , .

+2

. Sql. , ,

+1

For completeness, I must mention another possibility - inheritance (aka. Generalization or hierarchy of categories):

enter image description here

+1
source

All Articles