Insert, update query

Hello,

I’m making post commenting system and I want to increment total comments row inside posts table while at the same time comment got inserted into comments table.
Should I perform two queries or is there another way to do it?

On insert and if that not fail, update. So yes make 2 querys.

1 Like

There are database specific things like triggers stored procedures and such which could do this but they are probably more involved then you want.

I would suggest that you simply count the number of comments whenever you need the count. The eliminates the need to keep the comments table and the posts table in sync.

Keep in mind that if a comment was deleted then you would need to update the count as well.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.