views:

77

answers:

8

Hello.

I'm designing a database with three tables: user, game and (probably) register. I want to represent that a user can be registered on zero or more games.

I use table register to register a user in a game.

I also need to store the score of the user in a game.

My question is:

Can I add a column called score in table register? Is it a good design?

Thanks.

+1  A: 

You can put a user id and a game id into the register table, along with with a score. This will allow you to have a user registered for many games, each with a score. I think that will probably do what you want, if I understood your question correctly.

Michael Goldshteyn
+2  A: 

What you're describing is a many-to-many relationship, using REGISTER as the cross-reference table:

USER
UserID

GAME
GameID

REGISTER
UserID
GameID
Score
[+ registration info]

Sounds okay, though if either game-related or registration-related info gets more complicated you'll probably split it in two eventually.

Edited to add: Often you'll end up wanting to save game history ("average score", "last 5 games", "fastest time", etcetera). Then you have a one-to-many relationship between REGISTER and a GAME_HISTORY table.

Also, I agree with the answer that said calling it a 'registration' table will be confusing if it contains non-registration info.

egrunin
Thanks for your answer. I'm very new on database design. Reading your answer I have another question: Why would have to split the table if I'm going to add more information? And how can I split it in two?
VansFannel
A: 

I imagine, that you are going to use register table to avoid many-to-many relation between game and user. If you want to keep score of particular user in particular game, I think that's good solution.

kMike
+3  A: 

Yes it is an ok design. But you should probably change the naming.

user:
    userid
    username

game:
    gameid
    name

usergame:
    userid
    gameid
    score
klausbyskov
That's good suggestions, since register (or usergame in this solutions) is meant to solve many-to-many connection between game and user.
kMike
The naming is very important in my opinion. I would strongly recommend `usergame` (or, even better in my opinion, `user_game`) over `register`. The name `registration` (since it's a noun) would even be better than `register`.
Jason Swett
A: 

If it's a table that keep track of user score in different games: sure, you can have that column. But perhaps call it 'score' instead? (Unless you need some other information there, then perhaps you should use a fourth table)

erikric
A: 

I think you should consider creating a "relationship" table.

UserId | GameId | Score

UserId and GameId would be the primary key as well as each being foreign to the User and Game tables, respectively.

Brad
A: 

It is probably an appropriate design, as long as each register can have at most one score. If registers might have no scores, then that would correspond to a value of NULL. If this might be a significant proportion of rows in the table, then another table to hold scores might arguably be appropriate.

In an app I made, I had a similar situation, and because I wanted to add "high-score table" functionality (which necessitates ORDER BY queries on the score column), I ended up with quite a lot of indexes on my "register" table (more than one just for this purpose, because I had to have different indexes for different types of game). In the end I made a new table for scores, even though most registers do have an associated score, in order to make maintenance of these indexes less of an issue.

Hammerite
A: 

If the goal of your database is analytics then you'll also consider this:

Keep in mind that you'll be doing an Outer Join unless you include a row in the game table for 'no games registered' and put that as a default in the usergame table. You can delete that "non-registration" with the first registration... and add it back if the last registration is ever deleted.

This will make your reports inner joins and finding all the users without registrations is simple and obvious.

Stephanie Page