Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Users group table schema

I am creating a system where users can join and create groups. Within these groups, members can create topics and respond to topics already created, so I'd like to know your opinion on which of the following methods is best: Create two tables, groups_posts and group_topics:

--group_topics
id PK
group_id int FK
user_id int FK
title varchar(50)
content varchar(500)
status int

--group_posts
id PK
topic_id int FK
user_id int FK
content varchar(500)
status int

or create a unique table, group_tposts:

--group_tposts
id PK
group_id int FK
user_id int FK
is_topic boolean
title varchar(50)
content varchar(500)
status int
like image 558
jonathancardoso Avatar asked Dec 17 '22 13:12

jonathancardoso


2 Answers

Based on your description, seems to me that a bit more may be needed. Maybe something something like:

  • One group can have many users.
  • One user can belong to many groups.
  • Within a group users create topics (topic = subject; theme; a category or general area of interest.)
  • Within a topic users create new posts or reply to previous posts.

enter image description here

Note: add the content field to the Post.

like image 167
Damir Sudarevic Avatar answered Dec 19 '22 08:12

Damir Sudarevic


It really depends:

Case for the combined table
Given the fact that the boolean only takes one byte, the combined table is not a bad idea.
Do note however that creating an index on the field is_topic will not work because of its low cardinality.

MySQL (or any other database) will refuse to use an index on that field, but do a table scan instead.

Case for separate tables
If you plan use posts separate from topics often, I'd suggest separate tables.
Because the selects on those will be faster, MySQL does not have to do a full table scan to separate the posts from the topics.

like image 22
Johan Avatar answered Dec 19 '22 08:12

Johan