Is there a need for an unsigned integer id for one to one relations?
There is a table of users (users), there is a table with fields-counters (user_counts)
normal users like everyone
user_counts with fields: id, user_id, comments, likes, votes, notifications
UPD: user_id-unique, there could be 2 records with the same user_id user_counts
I wonder how necessary an unsigned integer id for the table user_counts if everything is still fetching/updates occur on user_id? Maybe it helps some of the internal operations of the database engine be faster or can be removed?
2 answers
in this particular case user_counts stores aggregated values comments, likes, votes, notifications and there is clearly no need a surrogate key, because the user_id uniquely identificeret record.
p.s. read what is a surrogate key, why you need it.
It is common practice when designing tables. Is primary key is foreign key. In this case, the user_id in the table. The primary key must always be unique. If not ID, then what do you take as the uniqueness of all rows? Otherwise we'll have to take comments or votes or Likes or notifications, or a combination of them. But where is the guarantee that you will not get 2 the same combination of for example from to comment notifications.? Yes, and put the key with the same comment xxx characters? ))
I hope will explain clearly )
Find more questions by tags MySQLDatabases
take for example a more normal implementation:
- forget the plural, each comment is one line with its own unique ID, which is tied to user_id
for example, in users - user_id = 1
Table comments:
id, user_id, comment
id, user_id, comment
id, user_id, comment
translate this into values
1, 1, privet
2, 1, poka
3, 1, kak dela
select comments.comment from comments where users_user_id = comments comments.user_id
as you can see user_id in comments is not unique, it's the binding post with a table of users. - thalia.Hermann50 commented on June 8th 19 at 16:43
user_counts stores just cached number of different actions of the user - Tod.Erdma commented on June 8th 19 at 16:46
I certainly do not know what are your knowledge, so it's hard just to answer. Could there be some specific situation or problem, and you just can learn)) - thalia.Hermann50 commented on June 8th 19 at 16:49
Users table
id | name | ... | posts_counts | comments_count | ...
And when you create new review create a review and incremental users.comments_count. I purely for convenience I decided to separate the number into a separate table and separate recording key user_id. That is, the increment is now happening here
increment user_counts.comments where user_id = {userId}. Don't see a problem. I would be glad if you tell me what is wrong in this approach :) - Tod.Erdma commented on June 8th 19 at 16:55
For example, in the toaster in the profile also displays the number of (Questions, answers). I doubt that every time they recalculate
Did not understand where the problem is in the architecture here. - Tod.Erdma commented on June 8th 19 at 17:01
I don't understand the table. Then everything is OK))
Still profile look in fact not often, but every comment, every like, every action will overload the system with additional insert to the table counters. But by itself, the count function is not integrated, so there I'd be thinking that you are rational that is)) - thalia.Hermann50 commented on June 8th 19 at 17:04