C
C
codercat2018-02-03 16:06:31
MySQL
codercat, 2018-02-03 16:06:31

Is there a need for an unsigned integer id for a one to one relationship?

There is a table of users (users), there is a table with counter fields (user_counts)
users is the usual one like all
user_counts with fields: id, user_id, comments, likes, votes, notifications
UPD: user_id -- unique, there cannot be 2 user_counts records with the same user_id
I wonder how necessary an unsigned integer id is for the user_counts table, if all the selections / updates still occur by user_id? Perhaps it helps some internal operations of the database engine to run faster or can it be removed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lazy @BojackHorseman MySQL, 2018-02-03
@codercat

in this particular case, user_counts stores the aggregated values ​​of comments, likes, votes, notifications, and there is clearly no need for a surrogate key, because the user_id uniquely identifies the entry.
ps read what is a surrogate key, why is it needed.

T
Therapyx, 2018-02-03
@Therapyx

This is a common practice when designing tables. There is a primary key and there is a foreign key. In this case, the user_id in the table. The primary key must always be "unique". If not ID, then what will you take as the uniqueness of all strings? Otherwise, you will have to take comments or Likes or votes or notifications or combinations of them. But where is the guarantee that 2 identical combinations will not come across, for example, from comments and notifications.? Yes, and put the key with the same comment xxx characters? ))
I hope I explained clearly)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question