Guys, help me figure it out on my fingers. (simplified the situation to the point)

There are two tables. The first with the list of goods. Let's call it productTable in it there is a column productID and the rest. There is a second table with commentTable comments and of course also with the poductID2 column (so as not to be confused). Now you need to establish links tables. I suppose to put on-update: cascade and on-delete: cascade Further small confusion, this is the essence of the issue.

In which table should this relationship be established?

I go to the productTable and put the link productID => poductID2 or vice versa in commentTable and link poductID2 => productID ?

  • one
    The link points to the primary key in another, hence the second option - Mike

2 answers 2

A product without a comment can exist? easily Hence, he does not need to refer to anything.

A comment without a product? couple hardly. So he needs to refer.

This means commentTable (productID2) references productTable (productID) .

  • In this version, if I delete the product, the comments will also be deleted (at the cascade). And you can safely remove the comments? Right? (sorry, for being stupid, that's the way it is now, but for some reason he thought and overcame paranoia) - Zhenya Vedenin
  • one
    Of course ... unless someone else refers to them. - Akina

From the child table to the parent.