0

Having trouble wrapping my head around the right way to structure this...

I'm building an event management application with a judging function for a contest. There will be multiple rounds, each with multiple judges and multiple participants. Any given judge may judge in multiple rounds and any given participant may participate in multiple rounds. But from each round there will be only one score per judge per participant.

I've created the following tables:

 - user 
        - user_id
        - user_name
        - user_role_id
 - user_role
        - user_role_id
        - user_role_name
        - user_role_desc
 - round
        - round_id
        - round_name
        - round_loc
 - user_round (to join many-to-many users and rounds)
        - user_id
        - round_id
 - score
        - score_id
        - score_value
        - round_id
        - user_id_judge
        - user_id_participant

user_role defines judge, participant, admin, support, etc. I then have all users in one table with a role assigned - this is what's got me confused. When I define score for example this means I have to include 2 keys from the user table to define both judge and participant. Is there a problem in doing this I'm not thinking about? I was initially going to have separate judge and participant tables, but I thought my current structure was more in line with the "right way". What else am I not thinking of?

2
  • 1
    There is no reason this shouldn't work. Have you actually tried and got an error? Commented Apr 29, 2013 at 14:56
  • No, just a bit worried in advance. Thanks! Commented Apr 29, 2013 at 16:08

2 Answers 2

1

You can have as many foreign keys in a table as you have fields. What you can't have is one field being a foreign key for TWO different tables at the same time.

There's no problem with your 'score' table have the judge and participant fields both pointing back at the users table. e.g.

create table score (
    judge_id int,
    participant_id int,
    foreign key (judge_id) references users (id),
    foreign key (participant_id) references users (id)
);

is perfectly valid SQL. You might want to add some logic in the client and/or DB to enforce that a participant cannot be their own judge, but that's another matter.

Something like

create table broken (
   fieldname int,
   foreign key (fieldname) references table1 (id),
   foreign key (fieldname) references othertable (id)
);

is outright invalid. One field cannot be "foreign" to two different tables at the same time.

Sign up to request clarification or add additional context in comments.

Comments

1

Multiple foreign keys to the same table is perfectly acceptable. As with any foreign key associations, you just have to be careful with how you join everything in your queries as that can, in some cases, cause a performance problem.

2 Comments

Thanks, can you provide an example of what I should watch out for when writing my join querys? It won't be a huge database, roughly 200 user rows and 500 score rows are the largest data sets I anticipate.
With your schema, I don't imagine there will be a problem, but generally speaking, using ON to limit what gets joined is more efficient than joining everything an using WHERE, and having indexes on the columns you use to JOIN is important for larger data sets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.