1

I am developing an a game application and is debating which is the correct way to handle which user is the owner of a played game.

You can either play as an anonymous guest who only has to provide your location or as a registered user. All users are stored in the database. Once you complete a game a database entry is added with you score and I also want to log who played the game.

The table looks as follows:

USERS ( id | user_name | password | country ) GAMES ( id | user_id | score )

As of now I just store a reference to USERS(id) in GAMES(user_id), how should I handle anonymous users though?

One way is to store every anonymous user as just an id and location and then add a column to GAMES called anonymous_id. But then one of user_id or anonymous_id would have to be NULL for every game depending on if the player was registered or not.

Another way would be to just add all the anonymous users to the USER table and allow user_name and password to be null.

Is there a better way to handle this?

Any advice appreciated.

4

1 回答 1

1

如果您将国家/地区存储在其他地方,我不会在 User 表中为每个匿名用户添加一个事件。因为对于每个匿名用户,您将插入 2 行(用户中的 1 行,游戏中的 1 行)。

相反,你可以

GAMES ( id | user_id | score | country_id) 

user_id 和 country_id 都是可以为空的外键。

当注册用户结束游戏时,您存储 user_id。您可以将 country_id 保留为空,因为这将是重复数据的情况。当匿名用户结束游戏时,您只需将得分和 country_id 存储在 Games 中。

您可以通过在游戏中过滤来识别匿名用户user_id is null,并且仍然可以按用户和国家/地区进行排名。

于 2013-04-18T11:04:29.437 回答