Combine two sources of friends relationships in one table - mysql

I have mobile app with users. Everything is simple - each user have id and name.
Users Table
id name
1 John
2 Dave
3 Jack
Some of this users are friends and the main purpose of my question - finding the best way to rebuild friendship table that looks like (the only rule is user_id_1 < user_id_2)
Friendship table
id user_1_id user_2_id
1 1 2
2 1 3
3 2 3
How I know that users are friends? I take this information from different sources (social network A, social network B). So if two users are friends at least in one of two social networks - they are friends in my app.
Right now I am recreating friendship table each day:
Take each user, create empty array NewFriends for this user
Erase all records containing his id from friendship table
Find all his friends in network A (A-friends)
For each of A-friends - find they ids in my app and add them to NewFriends array
Find all his friends in network B (B-friends)
For each of B-friends - find they ids in my app and add them to NewFriends array if they don't already exist there
Delete all friendship records from Friendship table that are not mentioned in NewFriends array
Insert all friendship records from NewFriends array to Friendship table that were not existed there before
How can I solve this task better?

First,
you can use the only one table of the 2 networks (instead of using A-friends table and B-friends table)
and when select you could use distinct keyword
steps will be :
Take each user
Erase all records containing his id from friendship table
Find all his friends in network A and add it to (Temp-friends)
Find all his friends in network B and add it to (Temp-friends)
For each distinct of Temp-friends - add records in NewFriends table
Delete all friendship records from Friendship table that are not mentioned in NewFriends array
Insert all friendship records from NewFriends array to Friendship table that were not existed there before

You can use set data type instead array. The reason to use set is set doesn't contain duplicate values.
Use two sets oldFriendships and newFriendships.
From friendships table load data to oldFriendships
Create new newFriendships
2.1. From network A find all friends for the user and add it to the newFriendships
2.2. From network B find all friends for the user and add it to the newFrindships
Update friendship table
3.1 Find oldFriendships complement newFrindships - this is removed friendships, delete these values from friendship table
3.2. Find newFrindships complement oldFriendships - this is added friendships, add these values to friendship table
Here is wikipeda article about complement

Related

How to store multiple user IDs for each "event" ID in MySQL

I'm totally new to MySQL.
I'm working on a university courses system where each course ("event") has many students("users") that are signed in to that course.
I want a to build a MySQL table that for each course ID will store all the students IDs that are signed in to it. I found two approaches:
Serialize students IDs (using json for example)
Create a new row for every new student with course ID and student ID
The first approach has a performance issue because of serialization/de-serialization of students IDs, and the second approach will cause the table to explode very quickly.
What do you think I should do? Is there a better solution?
Thanks!
Your case is a Many-to-Many mapping.
You would have 3 tables.
User
Events
User_Events
The third table will store the mappings, it should consist of two columns, user_id and event_id
user_id would be the foreign key for the User table, similarly, event_id would be the foreign key for the Event table. You can then retrieve data by Joins in queries.
so if you have users with ID 1, 2, and 3
and similarly courses with ID 1, 2, 3, 4
Here's how you will represent students that are signed in a course.
user_id, event_id
1 2
1 3
2 1
3 4
This means, user 1 is signed in course 2 and 3, user 2 in course 1 and user with ID 3 in course with ID 4.
Each student/user can be in several courses/events.
Each course/event can contain several students/users.
So you need a bridge table containing all relations:
user_event (userid, eventid)
with a unique constraint on userid + eventid.
(You can add a technical ID (e.g. user_event_id) to the table, but that's not necessary.)

Get a list in MySQL

I'm creating a website and users can add friends. I want them to be able to see their personal friends on a page.
For exemple:
John add user Tim and Bill.
When John goes on his friends list page, I want him to be able to see that he has Tim and Bill. How do I do that? Is that even possible? Do I need more than one table? If so, does every user has to have his own friendsList table?
Yes this is possible, you do this by querying the information from the database, the answer for if you need multiple tables etc all depends on your current table structure but at the very least you need to have some way of referencing that a Person 'John' has friends, wether thats just a 'friendID' in the same 'Person' table, or another means of doing so. then it is just a matter of querying the data correctly to return what you want and bind to the websites fields :D
One way of defining the structure is the following:
Person
PersonId
Name
<other person fields>
Relationship
RelationshipId
Name --> allow to define multiple relation types like Friendship, Follows etc.
Relationship
RelationshipId
Person1Id --> FK to Person
Person2Id --> FK to Person
RelationshipTypeId --> FK Relationship
Basically, you use an n:n between Persons (anyone can have any number of friends) and also allow for other types of relationships.
Assuming you already have a table of users, one approach would be to create a "friends" table which relates users to other users.
CREATE TABLE friends (
`user_id` INT NOT NULL,
`friend_id` INT NOT NULL
);
Both user_id and friend_id would have foreign key constraints on your existing users table (so that you guarantee an id must exist in your user table in order for it to exist in the friends table as either a user_id or friend_id).
You can then link your user table on users.id = friends.friend_id to get the friend's info.
Here is a SQL Fiddle Demo showing how this works.
You should consider using an ON DELETE CASCADE constraint on the friends table, so that if a user is deleted from the user table, the associated records in the friends table are also deleted.

How to insert values of one table into another

I have two tables user and friends where users from user table can become friends.I have inserted 3 users into user table,now i want to make friends out of for example user_id = 1 and user_id = 2 but i don't know how to add this two id's in friends table.Photos of these two tables are attached.
... I don't really know what software you are using, but what you need is to declared a foreign key "UserId" column in your friend's table. Once that is done you can insert data in anyway possible by either using Mysql or a simple SQL insert:
INSERT INTO `friend`(UserId) VALUES (1)

Prevent duplicated ID on MySQL (InnoDB) across 2 tables

Is it possible to have the ID of the next generated row (across 2 tables) be unique?
I have 4 tables:
1 for teachers
1 for students
1 for projects
1 for relations
The relations table has 3 foreign keys.
One refers to teachers IDs, one to students IDs and the other to projects IDs
Since a project can be related to teachers but also students at the same time, how do I make sure that a new created teacher or student won't have an ID already used by the other type of account?
If I can do that, then the relations table would have only 3 columns:
ID, project_ID and related_to(ID)
If not, I would have to add a 4th row indicating the type of account that it relates to (student or teacher).
Thanks for your help!
Regarding the difference between account types:
I have to translate this exact same situation to another project of mine in which the first two tables are completely different. That's why I don't bother to merge the students and teachers tables here.
You do not need to have unique values between the student and teacher tables because the relation table has separate fields for each relationship, so there is no conflict.
However, this is not the right way to do things. You need two relation tables, teacher_project and student_project. Alternatively, depending on the unique data that's different between teachers and students, you could have a single person table and a single relationship, which is probably closer to the real world anyway.
I think you can identify the teachers begin with 1 ,incremental 2; the students begin with 2 ,incremental 2.By this way,odd number refers to teacher while even number refers to student.No conflict will happen.

Friendship table

I've got a table called users which contains the users on my webpage. The users can make a friendship where user 1 request user 2 for a friendship.
The way it is working now is, that when user 1 sends the request I create a row in the friendship table which contains the two foreign keys: user_id_from and user_id_to and a field called status which can either be 0 (the request is pending), 1 or 2 either the user_id_to has accepted or declined the friendship request, respectively. And lastly the status can be 3 which indicates that one of the users have deleted the friendship.
Is this the right approach, or should I create a temporary table which contains all the pending request, and then (if the request is accepted) it is inserted into a friendship table?
From a logical perspective, what you're doing is correct, since I would personally consider a pending friendship request a form of friendship which isn't confirmed.
From a design perspective, I'm not sure how you would create a temporary table each time the user needs to see his list of friendship requests. If the table isn't temporary, you would have a table named confirmed_friendships and pending_friendships.
I highly suggest that you keep both in the same table, unless there will be lots of columns which are specific to pending_friendships and not available for confirmed_friendships, or vice versa.