How to design "like on posts" in relational databases? [closed] - mysql

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 11 months ago.
Improve this question
I'm developing a kind of social app, where an user can write a post, and give like on other posts.
And on the frontend users can see how many likes a post has.
In this case, how can I handle the count of likes for each post?
What I've been thinking so far is...
(1) maintain the post_likes table whose primary keys are user_id and post_id. Everytime users give like on a post, a new row is appended on the table. The count of likes are calculated from counting the corresponding rows of post_likes table.
(2) posts table has a like_count column. Everytime users give like on a post, the value of the column increases by 1. The count of likes are calculated from selecting like_count value.
I think that the implementation (1) would be better in that which user gave like to which post. However, it seems that it could be inefficient since select COUNT(*) should be executed in order to get the like count whenever users requests a page.
what is the best approach in this case?

Both approaches have their pros and cons, but I think approach (1) is a better option for several reasons:
You can prevent users from liking their own posts (if you want to implement that).
You can allow users to remove their like.
You can prevent one user liking a post multiple times.
If performance is an issue you can opt for a new table post_likes_count that will periodically be updated from post_likes with the new like count for a post. Again, keep in mind it depends on what you specifically want to accomplish.

Related

Best Practice for Budgeting MySQL Database Design [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to find the best solution for my needs. I have a budgeting application that each user will have their own budget which will include potentially hundreds or thousands of budget entries. Initially I thought I would have a table for the users and basic info, and then each user would have their own sql table with all of their budget items held within. However, if the user list grows to hundreds or thousands then I would have a large amount of tables since each user would have their own. I also considered a single table to hold everyone's budget entries but I'm not sure that's the correct solution either.
Any advice? Thank you in advance
I think a single table that holds all the budget entries, with a primary key that's referenced by a foreign key in the "users" table, is a good way to go. You can always use something like select * from users u join budgets b on u.userID = b.userID where userID = xxx to get the budget items for a particular user.

SQL task from job interview [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was recently on one job interview and one question was about databases which makes me problem for a bit.
This was task:
You are creating database scheme for bloging service where:
Every user has one blog
Every blog can have many different categories of blog posts
Every blog can have blog posts
Every post can be multiple categories
And questiones where:
Write select query containing 2 users with the most posts
Get a post which includes most categories
I have created database with datatables - Users, Blogs, BlogCategory, Categories, Postcategory and Posts
BlogCategory and Postcategory are relation tables mapping relation between blog/post and its category and I think this is correct database scheme.
And about these questions, I think that both will be some complicated queries with inner joins but so far I was not able to solve them. Any idea please?
I am trying in now in firebird db, because MS SQL server has problem installing ms sql management studio. Thing is that you have come up with your own tables with their columns and keys. For sure you will need User, blog, post, category table, next tables are up to you
Pretty vague to say the least but pretty sure something like this should work.
select top 2 UserName
from Posts
group by UserName
order by count(*) desc
select top 1 PostID
from Posts p
join PostCategories pc on pc.PostID = p.PostID
group by p.PostID
order by count(*) desc

database normalization a one to one relationship [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have two tables in my db:
Marketers:
Id, Username, Password, Email
Stores:
Marketer Id, 1 Store, 2 Store, 3 Store, 4 Store, ...., 10 store
there is the names of 10 stores for every marketer in Stores table.
So there is a one to one relationship between these two tables. right?
I'm wondering if it would be better to just combine these two tables or not.
I wan't to send a lot of query for the second table (Stores tables). so I though this would be better if I separate these two cause I rarely need the information stored in 'Marketers table'.
From a good design perspective, you should keep these tables as separate.
for your current requirements,
if you do not need data from Marketers so often, why do you need to include that in Stores. you would just end up fetching extra data each time.
say if tomorrow if some new info and the mapping changes to one to many or vice versa, your current design will work perfectly fine.
and of course from future maintainence view, it is easier to update current design.
although, i would also, suggest you to add an independent primary to Stores table also.

MySQL user data storage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have two tables that should somehow be associated. Let's call them table_a and
table_b. A row in table_a can be associated with multiple rows in table_b, and the same goes the other way around. How could I achieve this? Should I use a pivot table?
Both tables have an auto-incrementing id-column.
What you're looking for is called a many-to-many relationship (a given user has zero or more games, a given game has zero or more users). This is typically handled with a "mapping table", e.g. USER_GAMES which has a user_id and a game_id, uniqueness is on the combination of these. http://www.joinfu.com/2005/12/managing-many-to-many-relationships-in-mysql-part-1/ has some good details.
As it is a many to many relationship, an intersection table with the user ID & game ID would be the best. Otherwise you would have to parse the list of game ID's stored in the user table and that would cause performance issues.

Football Database Scheme [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm currently trying to build a football database in MySQL. It should store the fixtures from different leagues, plus their results, odds and some other information.
Is the scheme I just created correct or are there any mistakes in it? If so, what can I improve? I'm also not really sure about the link between tblMatch and tblTeams.
Afterwards I want to be able to make a Query where I can select a fixture including the points the home and away team got before the match, plus the average amount of goals of the teams. Like the new fields: 'homeTeamPoints', 'awayTeamPoints' ect.
So my question is: Where should I put these fields? In an extra table or should I put those in the table: 'tblMatch' and store the precalculated values there?
I hope you get what I tried to explain.
Best Regards
-bababow
A few notes:
You will want to replace "homeTeam" and "awayTeam" with "homeTeamID" and "awayTeamId" which will be foreign keys to the tblTeams table. This will enforce that the teams in the match both actually exist.
Remove the matchID and competitionID from the teams. I'm assuming teams can participate in many matches and competitions and therefore this structure will not support that.
What do you want to know about competitions? Is this a tournament? You may want to have a "bracket" and/or "tournament winner" column in there to store the results of the overall tournament.
Those are my main thoughts, other than that it looks OK.
In my perspective if the values of both the fields needs update regularly and table tblMatch data size is large then you should take it into separate table. if both the fields are updates whenever whole record is change then it could be in tblMatch table.