P
P
pemi2017-07-15 21:09:28
MySQL
pemi, 2017-07-15 21:09:28

Do you need a primary key for this?

There is a table-binding user-social network. The structure is:
user_id , service_id , id_in_service .
Do I need a primary key for this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2017-07-15
@melkij

primary key is always needed.
If, according to the logic of your application, only one user in each service_id can correspond to one user_id, then PK for the first two fields.
If there are several, then pk into 3 fields.
The order of the fields when specifying the PK of the link table is important. Very important for mysql/innodb, because the table is clustered by PK, and any access to the secondary key results in a search by PK (except for covering indexes). Choose the order based on the most frequent requests to the table, so that the maximum requests can go by the primary key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question