Home > Software engineering >  Determing relationship between comments and users SQL
Determing relationship between comments and users SQL

Time:07-28

I am mostly a front end guy that is messing around with SQL, and I am having trouble determining whether the diagram I am making is a "Many to One" relationship, or a "One to One" relationship.

Table comments {
  id integer [pk]
  user_id integer
  text text
  post_url varchar
}

Table users {
  id integer [pk]
  user_name varchar
  email varchar
  password varchar
}

My current thought is that the comments table is a "Many To One" relationship with the user table. For example, many comments can belong to one user. Is this correct? Or is it a "One to One", seeing as one comment can only have on user.

A little confused with this stuff, so any input would be appreciated. Thank you.

CodePudding user response:

It's best to express a relationship in both directions:

  • Each comment belongs to one user
  • Each user can make 0 or more comments

This relationship would be seen as a many-to-one relationship. A one-to-one relationship means one-to-one in both directions, while a many-to-many relationship means one-to-many in both directions.

CodePudding user response:

It is many-to-one relation from comments to users as you mentioned many comments MUST belong to one user and thats why you specify user_id in comments table. If the one-to-one is the case for comments and users, you have to specify comment_id in users table and this will cause always only one comment will be in your datum.

  • Related