How to save a list in a database

How do I save an int list in a web application?

I am developing an application similar to facebook, and for each user I need to save the identifiers of his friends, so csv in the DB database I do not think this is indicated given that the user camera even has 1000 friends and the database field that should contain this the list is fixed, csv can overflow this field.

Should I store these lists in local server files or is it best to use a database?

+4
source share
4 answers

The many-to-many relationship is usually handled using a separate table.

user | friend --------------- 1 | 2 1 | 3 2 | 3 etc 

Then you use JOIN to find out who is friends with this user.

+15
source

What you are looking for is a connection table connecting users to itself. The entry in this table will represent the friendship between one user and another.

User table

 UserID | UserName | FirstName | LastName ... 

Friends table

 ID | UserID | FriendID 

Both UserID and FriendID will be foreign keys in the Users table. You probably want to have a unique constraint for the pair (UserID, FriendID) and a non-unique index for the UserID. Note that UserID is PK for the Users table, and the identifier is PK for the Friends table. Using a separate PK table for friends will make it easier to access a specific user / user pair in your interfaces.

+5
source

You cannot save the list in one database field — you cannot easily query it. Multi-valued columns are almost always the wrong choice.

As for database design, you should have a many-to-many table for connecting users to friends (you can also keep the corresponding opposing relationships in your table to easily bypass bidirectional communication).

+2
source

Use the database.

Today you can have 1000 friends.

Tomorrow, 10,000. Obviously this does not scale very well using only CSV.

+1
source

Source: https://habr.com/ru/post/1333398/


All Articles