Many to many relationship and MySQL

user2019594

If I wanted to make a database with subscribers (think YouTube), my thought is to have one table containing user information such as user id, email, etc. Then another table (subscriptIon table) containing 2 columns: one for the user id and one for a new subscriber's user id.

So if my user id is 101 and user 312 subscribes to me, my subscription table would be updated with a new row containing 101 in column 1 and 312 in column 2.

My issue with this is that every time 101 gets a new subscriber, it adds their id to the subscription table meaning I can't really set a primary key for the subscription table as a user id can be present many times for each of their subscribers and a primary key requires a unique value.

Also in the event that there's a lot of subscriptions going on, won't it be very slow to search for all of 101's followers as all the rows will have to be searched and be checked for every time 101 is in the first column and check the user id (the subscriber to 101) in the second column?

Is there's a more optimal solution to my problem?

Thanks!

Carsten

In your case, the pairs (user_id, subscriber_id) are unique (a user can't have two subscriptions for another user, can they?). So make a compound primary key consisting of both fields if you need one.

Regarding the speed of querying your subscription table: think about the queries you'll run on the table, and add appropriate indexes. A common operation might be "give me a list of all my subscribers", which would translate to something like

SELECT subscriber_id FROM subscriptions WHERE user_id = 123;

(possibly as part of a join). If you have indexed the user_id column, this query can be run quite efficiently.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql Many to Many relationship

From Dev

denormalize many to many relationship in MySQL

From Dev

Querying many-to-many relationship in MySql

From Dev

MySQL, join many to many relationship query

From Dev

MySQL filter many elements from many-to-many relationship

From Dev

Is this a many-to-many relationship?

From Dev

Many to Many relationship in Ecto

From Dev

implementing a many to many relationship

From Dev

Many to many relationship optimization

From Dev

many to many powerpivot relationship

From Dev

Sqlalchemy many to many relationship

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship with itself

From Dev

Many to Many Relationship in Cakephp

From Dev

Searching many to many relationship

From Dev

A many to many relationship

From Dev

What is a many to many relationship?

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Django many to many relationship

From Dev

How to query for many to many relationship between products and filters in MySQL?

From Dev

How to do MySQL CRUD select on a many to many relationship

From Dev

MySQL - How to insert into table that has many-to-many relationship

From Dev

Best way to implement many-to-many relationship in MySQL

From Dev

Finding voters that did not vote in an election through a many to many relationship with MySQL

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

MySQL many-to-many table relationship with three entities

From Dev

need help creating a simple many-to-many relationship using mysql

From Dev

php mysql many to many relationship query result display

Related Related

HotTag

Archive