mysql- Database schema of messaging application with different group - php

I know how to make a simple messaging schema for one to one user messaging ( all user come from same table) but my problem is that I have multiple Table with different type of users which have their primary keys now when a user from one table want to send a message to a user from another table then what should be schema for this application,
Let I have these three tables with different type of users-
TableA- id | Name |...
TableB- id | Name | ...
TableC- id | Name | ...
Now a user from TableA want to send a message to a user from TableB or TableC then what should be database schema for messaging app (all three tables can have different user with same primary key i.e id)

Storing the same types of data with slightly differing properties is antithetical to databases. You can make it work by pointing to Table.ID within your application, but it's not pretty and not best practice.
What you're looking for is a single users table with one or more flags or references (more columns/fields in the table) indicating what details are different for each user. If you need multiple user groups, you should simply have groups table.
eg.:
# users table
CREATE TABLE users (
user_id INT,
user_name VARCHAR(32),
group_id INT,
talk_outside_group BOOL
);
# groups table
CREATE TABLE user_groups (
group_id INT,
group_name VARCHAR(32),
);
INSERT INTO user_groups SET group_id = 1, group_name = 'PHBs';
INSERT INTO user_groups SET group_id = 2, group_name = 'HR';
INSERT INTO users SET user_id = 1, group_id = 1, user_name = 'Alice';
INSERT INTO users SET user_id = 2, group_id = 1, user_name = 'Bob';
INSERT INTO users SET user_id = 3, group_id = 2, user_name = 'Carol';
INSERT INTO users SET user_id = 4, group_id = 2, user_name = 'Dave';
In this example, Alice and Bob are PHBs and can barely work their keyboards.
Carol and Dave are HR drones and have boring conversations ;)
When and whether users of separate groups can talk to each other could be indicated by a flag in either table (depending on how granular you need the privilege control... in this example, the talk_outside_group field) and should be handled in the application layer.

Your users shouldn't be in different tables.
If you want some users to have privileges then do a table "privileges" and link it to "users" with a table "user_privilege" which would contain "id_user" and "id_privilege".
If you really want to do what you are doing, then you'll have to add a field "table_name" in the message table to be able to know from where the user is coming.

Related

Questions about my Database MySQL and Android Studio

I want to make an app that show list of quotes to the user, and each user can favorites the quotes they like, so that the user can see all of the favorites quotes that the user favorited.
I want to make the app on Android Studio and using MySQL, not SQLite.
So if I want each user to have their own favorites list, then I must make a user table right? for login?
Is this table right?
Table User
user_id (Integer,Primary Key, Not null, Auto Increment)
email(Text or varchar?? , Not Null)
Password(Text or varchar??, Not Null)
Table QuoteList
quote_id (Integer, Primary key, Not Null, Auto Increment)
quotes (text, Not Null)
author (varchar(30), Not Null)
genre (I want it to have multivalue, like : motivational,love)
My Questions is:
What's the correct type for email,password, and genre?
Where do I put the Favorite Column, the Table User or Table QuoteList?
The Favorite column should be boolean with default 0 right?
EDIT POST
So I will have to make three tables:
User Table
QuoteList Table
UserFavourite Table
The User Favourite Table have two foreign keys, that is:
user_id
quote_id
and so, here is the example of populated userFavourite Table
user_id quote_id
1 2
1 3
1 4
2 4
2 6
Is it like that?
Email, password and genre can be varchar.
Since you want the members to choose their favorite quotes, you need to create a relationship between User and QuoteList table.
You can create another table
UserFavorite
user_id
quote_id
user_id and quote_id comes from the other two tables. This way you know which user favorites which quote. user_id comes from the logged in user and quote_id comes from any quote from the list of quotes.

Users History and Users saved database schema design

I want to store into database the visited users and favourite users (when a user clicks on hearth icon). So, I could create 1 table, users_activities like so:
Users_activities:
id_users (pk)
id_users2 (pk)
activity (varchar or enum, it'll contain "history", "favourite") (pk)
created (datetime)
For the history.php or favourites.php pages, I'll select all, where id_users = X and activity = "history/favourite", join users table. For insert and update, I have a composite primary key, users, users2 and activity. If I'll find a users X, users2 Y and activity Z, update the row..else insert.
Otherwise, I could create 2 separated table, users_history and users_favourite, like so:
Users_history
id_users (pk)
id_users2 (pk)
created
users_favourite
id_users (pk)
id_users2 (pk)
created
it's a simple design and SQL is tiny simple than first form. Which is better? I use php and mysql.
"History" sounds like a log of things. "Favorites" sounds like a current set of values. They have two different sets of usages and properties; do not put them in the same table.

Database Schema, PHP

I have the mysql tables
Contract
-**contract_id** (PRIMARY)
Contract_to_groups
-**group_id** (PRIMARY)
-contract_id
Groups_to_users
-group_id
-user_id
I have this multiselect(multiselect.js) where elem is the users
I have already the contract and I want to create a group of users. So I select my users from multiselectand then click submit. On submit in my table Groups_to_users should be saved the users I selected from the multiselect.
I have the problem that if I set in Contract_to_groups the group_id as AUTO_INCREMENT I cannot save in Groups_to_users the users for this group because I dont know the group_id of Contract_to_groups, it is AUTO_INCREMENT and especially if I have four(4) multiselects and each with users how can I save in these two(2) tables Contract_to_groups, Groups_to_users the data with the correct ids ?
Try to modify your Contract_to_groups table, and add an id which wil be the auto incrementing primary key:
-**id** (PRIMARY)
-group_id
-contract_id

How to link a row of MySQL database table to another row in another table

I know it makes little sense... and i'm new to using MySQL...
What i'm trying to do here is, link one tables row to another tables row...
for an example there are two tables..
one table is for user registration and same table is used for login as well...
and the next table is for user posts.. like status updates and all...
here is how i want it...
user_log_info:-
id ( primary )
firstname
lastname
username
email
password
posts:-
id ( primary )
userposts
posted_by
date_post
so as you can see, i want the user_log_info tables username to be automatically copied to posts posted_by row... And i have no idea how i can archive this...
You haven't given nearly enough information to give a full answer, but I'll do my best with what you've given.
Tables
+-----------------+ +-----------------+
| users_log_info | | posts |
+-----------------+ +-----------------+
| int ID (primary)| | int ID (primary)|
+-----------------+ | int posted_by |
+-----------------+
(I left off fields that are irrelevant to what you seem to want to do, I'm just simplifying it)
posted_by is an unofficial foreign key, or referencing the primary key of another table.
To insert, what you can do is along the lines of this:
INSERT INTO posts(...., posted_by) VALUES (...., user.ID)
Where .... is referencing all of your other information to insert
Then, to find information on someone who posted something:
SELECT * FROM users_log_info WHERE ID = Post.posted_by
Or if you want to find all posts by a user:
SELECT * FROM posts WHERE posted_by = user.ID
So, if Bob, who is User ID 3 wants to post "Hi", you might be able to do:
INSERT INTO posts(content, posted_by) VALUES('Hi', bob.ID)
And then when you are outputting the post you might do this:
post = (however you choose the post to put on the page)
userPosted = SELECT * FROM users_log_info WHERE ID = post.posted_by
print post.content + " posted by: " userPosted.Name
Essentially, the field "posted_by" is, to "posts" an arbitrary number, but you know that it links to, or references, a user. It does that by referencing "ID", which is the primary key of users_log_info, so that when you want to get information from users_log_info, is all you need to do is select the entry which has the ID that corresponds to "posted_by". I do recommend naming it something like posterID, however, for easier identification.

System for keeping track of user favorites

On my website, I have a table movies and a table users
I'm trying to have an "Add to favs" button that a user can click, which will add that movie to his favorites (ajax / javascript not necessary at the moment, just php).
So what's the simplest way I could do something like that? I've thought about this but I can't seem to find a solution (all I think of is way too complicated, and in my opinion not possible).
What's your thoughts?
I don't need a ready-made script, just an idea that could get me working (although if you have an example of such script, I'd be happy to look at it).
Thanks!
This is a many-to-many relationship. A user can favorite many movies, and a movie can be favored by many users. In an RDBMS, you represent a many-to-many relationship with a third table. I call this an intersection table but it goes by other names too.
Create a table with two columns. The columns are both foreign keys, referencing movies and users, respectively.
CREATE TABLE Favorites (
user_id INT NOT NULL,
movie_id INT NOT NULL,
PRIMARY KEY (user_id, movie_id),
FOREIGN KEY (user_id) REFERENCES Users(user_id),
FOREIGN KEY (movie_id) REFERENCES Movies(movie_id)
);
When a user chooses to favorite a movie:
INSERT INTO Favorites (user_id, movie_id) VALUES (?, ?)
When a user decides they don't like a movie any longer, delete the corresponding row:
DELETE FROM Favorites WHERE (user_id, movie_id) = (?, ?)
To get the set of movies favored by a given user:
SELECT movie_id FROM Favorites WHERE user_id = ?
To get the set of users who favor a given movie:
SELECT user_id FROM Favorites WHERE movie_id = ?
Regarding one of your comments:
You shouldn't make the "Add to favorite" a link. Indexers like Google will follow links, and then before you know it, every user has favorited every movie.
The general best practice is that read-only operations can be GET requests, while operations that write to the database can be POST requests. This means that you need to use a <form> element to submit POST requests, not an <a href="..."> element.
Add a third table:
CREATE TABLE user_favorites (
user_id INT NOT NULL,
movie_id INT NOT NULL,
PRIMARY KEY (user_id, movie_id),
FOREIGN KEY user_id REFERENCES users (user_id),
FOREIGN KEY movie_id REFERENCES movies (movie_id)
)
This is called an intersection table or join table, as it joins rows in the users table to rows in the movies table (as you see, each column is a foreign key). It is also defines a many-to-many relationship, because one user can like many movies and one movie can be liked by many users.
When you go to add a favorite movie for a user, all you have to do is insert a row in this table with the ID of the user and the ID of the movie:
INSERT INTO user_favorites(user_id, movie_id) VALUES([user ID], [movie ID])
To see what movies a user has favorited:
SELECT movie_id FROM user_favorites WHERE user_id = [user ID]
You will need to create a new table:
user_favorite_movies
--------------------
ID (primary key)
userID (foreign key)
movieID (foreign key)
date
Then when the user clicks the 'Add Favorite' button, you just insert a new row into user_favorite_movies with the users ID from the user table, the movie id from the movie table, and the date it was added (good for sorting later).
Hope this helps!
Best,
-Eric
You could create a table favourites with three columns, id, mid and uid. To add a favourite:
INSERT INTO favourites (mid, uid) VALUES (3, 5)
To search for favourites of one user:
SELECT * FROM favourites WHERE uid = 7
To search for people who favourited one movie:
SELECT * FROM favourites WHERE mid = 9
So far as I can see, you'll still need to use JavaScript or Ajax to do the post, unless you want to refresh the page every time thet mark/unmark a favorite, and also to add/remove the new favorite indicator in place at the same time.
Or am I missing something?

Categories