How to build a user/group/achievement database?

情到浓时终转凉″ 提交于 2019-12-11 08:24:22

问题


I'm working on a new web platform that has users, groups and those users can get achievements (each group has its own achievements) for doing specific tasks and these are the tables I have at the moment:

  • Users
  • Groups
  • Group_users (used to store user's groups, since users can join more than one group)
  • Achievements
  • User_achievements (used to store user's achievements, since users can have more than one achievement)

My question is, in the User_achievements table, should I store the user and group id's or only the group_user id that holds the relation between group and user?


回答1:


First off to answer your question, I would store just the user_id as a foreign key in the user_achievements table as the achievements are linked to the user not the group in this table.

If the achievements are also linked to the group, as you mentioned in a comment to another answer, that relationship should be shown in the achievements table, not the user_achievements table.

Also why would the Group_users table have its own unique id (which you seem to be implying that it is), is it not just a way to join a many to many relationship? If this is the case why not just make the primary key of the group_users table a combination of the user_id foreign key and the groups_id foreign key. There is no need for a unique id for the table, as the combination of the two foreign keys is unique in itself.

EDIT:

So I'd do it like this:

Users table:
    id (Primary Key)
    ... What ever other data you want to store for the user ...

Groups:
    id (Primary Key)
    ... What ever other data you want to store for the group ...

Group_users:
    user_id (Foreign Key to users.id) (Part of primary key)
    group_id (Foreign Key to groups.id) (Part of primary key)

Achievements:
    id (Primary Key)
    group_id (Foreign Key to groups.id)
    ... What ever other data you want to store for the achievement ...

User_achievements:
    achievement_id (Foreign key to achievements.id) (Part of primary key)
    user_id (Foreign key to users.id) (Part of Primary Key)



回答2:


I would advise you to use user and group_id in the users_achievement table. Reasons.

  1. While using the select query you'll have to make an innerjoin to get the user id which will take More time as SQL will have to look at both tables And perform an innerjoin
  2. As u said its for user_achievements And not groups_achievements definitely And a user_id should be there

:)




回答3:


1) Is it possible for an achievement to be associated with more than one user (or user_group)? If answer is more than one user, then you need a user_achievement table. Otherwise, you could just put the user id into the achievement table itself.

2) Is the achievement associated with a user_group or just a user? If the answer user_group, then store the user-group id in either a user_group_achievement table or directly in the achivement table (see first question). Otherwise, store the user id in user_achievement table or user_group_achivement table (again, first question).

users
-------
user_id
user_name


groups
-------
group_id
group_name


user_groups
-----------
user_id
group_id


achievments
------------
achievement_id
achievement_name


user_achievements
-----------------
user_id
achievment_id


group_achievements
-----------------
group_id
achievement_id

So you have many-many relations between user-groups, users-achievements, and groups-achievements.

you can do joins to find: all users in group all groups for user all achievements for user all users for achievement all achievements for group all groups for achievement

it would take an addition join to find all users associated with a group achievement.



来源:https://stackoverflow.com/questions/22181722/how-to-build-a-user-group-achievement-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!