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'm designing a database for social media which is similar to facebook structure. I'm using MySQL. The main goal for our database is the performance, it must handle a high number of requests.
I have a list of users, friends, posts on wall, comments and likes.
Q1- for users, I have several types of users, I have normal users, supervisors and admin. for that table, I'm thinking to define one parent user table and then inherit the information, but at the same time the fields are same.
example:
user
=============
id
username
password
email
isAtcive
country
noraml_user
===============
name
..
..
user_id
supervisor
==============
name
..
..
user_id
admin
=======
name
..
..
user_id
I considered this method because of performance, so instead of searching for single user using user type in (one million users as an example), I search for (300K users in table supervisor). Am I right?
Q2- I have likes for the posts and comments. here is my design
posts
==========
id
content
comments
==========
id
content
post_id
posts_likes
==========
id
post_id
user_id
comment_likes
==========
id
comment_id
user_id
Do you think this is correct, or I just make one table for likes such as the following
likes
=========
id
post_id(nullable)
comment_id(nullable)
user_id
what is best approach?
Q3- Could you provide me some tips to be considered for designing social network database?
Thanks :)
Q1: You shouldn't create separate tables for different user types. In fact, you should have a user role column in a common user table. This role would then define what the user can do. The whole point of indexes is to efficiently find subsets of a table.
Q2: Again, you'll probably find that you have content and likes (or maybe even "reactions"). Unless there's a specific reason to keep posts special, they're really just content that has no parent.
Q3: Yeah, so that's way to o broad a question for a site like this.
Related
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 8 years ago.
Improve this question
I'm trying to decide the best possible way to structure my user table. Users can have access to multiple "brands" and each brand will have multiple tables. That is, if user X wants to see data for brand Y, the database contains the information to say which tables I need to make calls to.
For example, user X can access Brand1 and Brand2. Brand1 has its data in table1, table2, and table3. Brand2 has its data in table4, table5, table6. User selects Brand2 and the application makes a call to find out that table4, table5, table6 should be used until user selects a different brand.
What's the best way to structure this knowing that a single brand might have multiple users that can access the data?
Do I need more than just a user table and, if so, what else and how would that connect to the user table?
Thanks.
Like Mark Baker pointed, you can have one user table, one brand table and one user_brand table.
user table - stores user_id (and other user data)
brand table - stores brand_id (and other brand data)
You've already defined relationships between users and brands. It's M:N (many to many), which means that:
one user can have access to multiple brands.
one brand can be accessed by many users.
Table user_brand solves the access problem.
user_brand table - stores user_id and brand_id (and optional data which better describes this relationship).
Here is an example about sql syntax (enforcing foreign key constraints).
You can use GRANT query so the user can access just 2 tables in a database, then in the application, you can code it just to select 1 table, until the user changes the brand. The brand itself is the table, isn't it?
In PHP code, the code and query should be like this:
<?php
$db = new mysqli('hostname', 'db_username', 'db_password', 'db_name');
$brand = $_SESSION['brandName']; // use this if you use sessions to cache the data
$db->query("SELECT * from `$brand`");
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 8 years ago.
Improve this question
At the moment I create a blog system with php. The admin can create x posts, and under every post should be a comment function. How can I realize something like that? I dont know the structure of the databse, because it must be many tables (for each post(?)). Maybe someone of you have a good link. I searched already but have found nothing for my problem.
You don't need many tables. You could have a table for the posts like this:
id INT(10)
title TEXT
content TEXT
And a table for the comments, like this:
id INT(10)
postId INT(10) // this links to the column post.id
content TEXT
Of course, you can add more columns to both tables if necessary, for example to add timestamps, etc.
Now, when you have a post with id 7, you make sure that all the comments to that post get postId 7. That way, when you display post 7 on your website, you can search for all the rows in the comment table with postId=7, and display exactly those comments that are linked to that post.
I've simplified this example. To optimise the tables you could add indices, primary keys, etc.
#Skeptar, there quite a number of ways you can achieve this. Let me suggest one to you.
Users table: (very obvious)
Columns: user_id (INTEGER(n) PRIMARY KEY), username (VARCHAR(n)), password (VARCHAR(32) for md5), level (INTEGER Example: 1 for admin, 3 for regular user)
Posts:
Columns: post_id (INTEGER(n) PRIMARY KEY), post_title (VARCHAR(n)), post (TEXT), user_id (INTEGER(n))
Comments:
Columns: comment_id (INTEGER(n)), comment(TEXT), post_id,(INTEGER FOREIGN KEY REFERENCES post(post_ID)) user_id(INTEGER(n) FOREIGN KEY REFERENCES users(user_id))
Now, I believe you have a means of allowing both levels (admin and regular) of users to register into your system.
When inserting, you take the post title and the actual post and with the help of some user identification mechanism like SESSIONS, you get the user_id and insert them into to post table.
Then for each comment, when inserting, you fetch the post_id of the post on which the user is commenting and insert that into the comment table along with the comment and the user_id.
When you want to fetch all posts, that's fairly easy.
When you want to fetch all comments for a particular post, you take the post_id of that post to the comments table as such:
SELECT * FROM comments C, posts P WHERE C.post_id = C.post_id;
Since you inserted the user_id against each post and comment, similarly, you can fetch the corresponding user information for each comment as such:
SELECT U.username FROM comments C, users U Where C.user_id = U.user_id
Remember, this is only to give you and idea and not to be taken as a complete working solution. I wrote this off head and could have some errors.
Also, where I indicated in bracket n (INTEGER(n)), it means you need to replace n with the desired columns size. Hope it helps.
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 8 years ago.
Improve this question
I am new in Laravel, I want to have like button on my website
My Post Table:
post_id | body | created_at
My like table:
like_id | user_id | post_id | timestamp
Questions:
1-How to count likes in Laravel?
2-Is my database structure true or not?
3-I am going to have reputation (Like stackoverflow), Should I save that republication in one field in user table or it has different structure?
You can count results with Laravel using count() method. For
example to count all likes in the table:
// Eloquent + Query Builder
$likes = Likes::all()->count();
// Query Builder
$likes = DB::table('likes')->count();
You might want to look at the aggregation methods Laravel offers.
The structure of the two tables you showed looks fine. One thing to consider is what should happen to respective likes when a user or a post gets deleted (ON DELETE ...)
You could put reputation field in the user table, but the cleaner way
to do it is to create separate table for reputation with a foreign
key to user ID.
I would add the total like count to the post table as well. If you don't add it you need to perform sum() queries all the time, which might put too much load on your database (depending on the numver of visits on your site, etc).
You can save the reputation in the user table. You probably want to add something like your like table though. Users for sure want to know why they received some more reputation.
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 8 years ago.
Improve this question
I am new to Php programming and I'm having some troubles understanding it. I need to make a project that will manage students and their grades. I need to have 2 types of users, professors and students. A professor can add grades to students, and a student can only view his grades.
I managed to make a simple login system, using wampserver and phpmyadmin.
Now I don't know how to represent the students and the professors (since the teachers can add grades I assume they are treated like some sort of admins and students are users).Should I put a checkbox on the login page? And after that I should create different pages to each type of user?
Do not get confused by my questions. I don't need any code just some ideas on how to get started. After that if I have any problems I will share my code also.
If you are using the same table for all users you can add one more column in your table with name 'is_teacher'. which could be 1 or 0.
In this way you can differentiate if the user is a teacher or student
This is all quite basic.
Assuming you have saved the users in your database, you simply have to add a column to the table those users are saved in. You could call this column 'flags'. If the user is a student, you could give it the value 1. If the user is a professor however you would give it a value 2.
In your code when you verify the login, i reckon you put their data in a SESSION. So now, with the new column present. Place that value in your SESSION as well.
Now that we can distinguish user and professor, we could simply do:
if ($_SESSION['flags'] == 2) {
// Something only the professor can do
}
Since you want some kind of admin system, make a row in your database table with an integer in it, In Example: student1 has INT 0, 0 will be student, teacher1 has INT 1, 1 will be teacher.
Then when you log in get this INT and paste it in a session, then let that session decide if the student or teacher page is loaded.
Thats what i would do at least :).
add one column type take enum(1,0); 1 for teacher and 0 for studant so that you can send type when adding user which you want and show the functionality as on its type.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i am working on site where users login to read articles, every articles is based in a category. so i want to display on the 'user home page' the category and article the last have visited/read. This counts for multiple category, so it neets to display "category 'a' read 12 of 20 articles 'click here to continue reading' " If someone can help me or send me in the right direction, any help / info is very much appreciated. Thank you
There's no special utility that can do something like that for you, if that's what you're thinking. What you will need to do is create a table in MySQL that stores a list of articles read by your users. Maybe a table with just user_id and article_id, together making up the primary key. INSERT IGNORE when a user visits an article, and when you want to get how many articles have been read, you can SELECT COUNT(*) FROM table WHERE user_id = ?
SELECT COUNT(*) FROM table WHERE user_id = ? AND article_id IN (SELECT id FROM articles WHERE category_id = ?)
The query above would let you get the number of articles that the specified user has viewed within the specified category, assuming you have it set up like this.
You also mentioned that you want to be able to select the most recent article or category the user has read. To do that, you can add a timestamp field to the new table you created. Another way is to add an auto_increment field and grab the biggest one.
Just an idea. You can create an extra table , lets call it user_read and store which user read which article.
Table user_read
user_id
article_id
I assume you know which article belongs to each category, therefor you can count how many articles in each category the user has read.