views:

34

answers:

1

I need some help in creating the best possible table schema for the following condition.

There is a competition where people are allowed to signup in groups. Each group has a groupid, each user has a uid, each group has group specific data like group_question_id etc. A single user can be part of multiple groups. Each user can answer the group question with group_answer_uid. The number of group_answer_uid (s) for user is equal to number of groupid is he is part of.

Data
====
1)groupid
2)uid
3)group_question_id (specific to each group)
4)group_answer_uid (specific to each group for each user)

Following queries have to be made.

1. list all groups the user is part of
2. list all users in a groupid
3. list all group_answer_uids for a given group 

What is the best schema possible for this?

+1  A: 

I would suggest you'd want something like...

USER table, keyed on user_id
GROUP table, keyed on group_id
USER_GROUP table, holding user_id, group_id pairs
QUESTION table, keyed on question_id
GROUP_QUESTION table holding group_id, question_id pairs
ANSWER table holding user_id, group_id, question_id triplets.

Then the queries would look like

SELECT group_id
    FROM USER_GROUP
    WHERE user_id = (our user);

SELECT user_id
    FROM USER_GROUP
    WHERE group_id = (our group);

SELECT answer_id
    FROM ANSWER
    WHERE group_id = (our group)

This assumes that a user can have a different answer to each question according to what group he is in.

Brian Hooper