2011-05-04 106 views
0

我正在设计一个数据库,我有如下结构:另一个数据库设计问题

是一个游戏数据库,我会有用户,游戏和组。

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). 

我怎么能代表一个用户创建了一个游戏?

UPDATE

游戏总是需要一个所有者。
游戏的拥有者将永远是一样的。
游戏结束时可以删除。

+0

Q1去:请问游戏的主人永远是一样的吗?例如随着时间的推移发生什么Q2:游戏是否需要拥有者才能存在,即游戏总是必须拥有一个拥有者? – trickwallett 2011-05-04 09:52:21

+0

我更新了我的问题,并提供了更多详细信息(和您的答案)。 – VansFannel 2011-05-04 09:59:32

回答

4

表示您所描述的所有权关系的最常见和最自然的方式是将组和游戏表中的外键添加到用户表中。

如果你这样做,你不应该在用户表中需要标志字段来表示用户是否是所有者。这可以通过查询来确定。

我与

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. 
+0

+1我会说这会起作用。 – 2011-05-04 10:03:24

+0

感谢您的回答。如果我使用这个,我会从用户到组的参考,以及从组到用户的参考。这是一个问题吗?我选择在用户表中添加GroupOwner列来避免这种情况。 – VansFannel 2011-05-04 10:12:55

+2

您应该可以同时拥有两个引用,一个表示成员资格,另一个引用所有权。如果用户始终是拥有组的成员,那么使用标志而不是外键确实可以识别所有者,但这更清晰。 – 2011-05-04 10:22:46

0

我想说的是添加一个用户ID字段到组和游戏表,这是指组的用户是游戏的所有者。