Relationships in BD

1

Hi, I have a question about a relationship and I would like to know if this is a wrong way or is there a better way? These are sample tables, suppose that a post is created and this post can have many comments and can also have many responses, but the comments can only have one response. MY doubt is if the key table_post_id is redundant since it is in the comment as well as the response

TABLE_POST
  id
  name

TABLE_COMMENT
  table_post_id
  table_response_id
  comment

TABLE_RESPONSE
 table_post_id
 response

    
asked by Ronny Medina 11.07.2018 в 17:07
source

1 answer

1

It's an interesting relationship, you can handle it in the following way:

        **POST**
        ID_POST
        NAME

        **COMMENT**
        ID
        COMMENT

        **RESPONSE**
        ID
        RESPONSE

And a table that relates them all

        **TABLA_RELACION**
        ID_PROPIO//ES OPCIONAL
        ID_POST
        ID_COMMENT
        ID_RESPONSE

Now, if a comment can only have a response, define a UNIQUE KEY composed between ID_COMMENT and ID_RESPONSE in the relationship table.

I hope it serves you!

    
answered by 11.07.2018 в 17:32