另一个数据库设计问题

时间:2011-05-04 09:46:48

标签: database-design

我想要一个数据库,我有以下结构:

是游戏数据库,我将拥有用户,游戏和群组。

A game will have 1 owner (an user). In other words a game will be created by an user.
A game will have 1..n groups.

A group will have 1 owner (an user). In other words a group will be created by an user.
A group will have 1..n users.

A user will belong to a group. The user can't belong to more that ONE group.
A user will belong to a game. The user can't belong to more than ONE game.

我不知道如何知道哪个用户创建了游戏。我想到了两种可能性:

  • 将游戏表中的外键添加到 用户表。
  • 在用户表中添加一个名为的列 gameOwner,如果用户将其设置为true 创造了一个游戏。

以下是我的表格:

User
--------------------------------------
Id                  | PK
Name                | User name (not null)
GroupOwner          | Bool type. True if user has created a group.
GroupId             | FK to group table. Can be null: user is not playing any game now.


Group
--------------------------------------
Id                  | PK
Name                | Group name (not null)
GameId              | FK to game table. Group belongs to this game.


Game
--------------------------------------
Id                  | PK
Name                | Game name (not null).

我如何表示用户创建了游戏?

更新

游戏总是需要拥有者 游戏的拥有者将始终如一 游戏结束后可以删除。

2 个答案:

答案 0 :(得分:4)

表示您所描述的所有权关系的最常见和最自然的方式是将Group和Game Tables中的外键添加到User表中。

如果执行此操作,则不应在“用户”表中使用标记字段来表示用户是否为所有者。这可以通过查询来确定。

我会去

User
--------------------------------------
Id                  | PK
Name                | User name (not null)
GroupId             | FK to group table. Can be null: user is not playing any game now.


Group
--------------------------------------
Id                  | PK
Name                | Group name (not null)
GameId              | FK to game table. Group belongs to this game.
OwnerId             | FK to User table. Group belongs to this user.


Game
--------------------------------------
Id                  | PK
Name                | Game name (not null).
OwnerId             | FK to User table. Game belongs to this user.

答案 1 :(得分:0)

我会说,在组和游戏桌中添加一个userID字段,该表引用作为该组或游戏所有者的用户。