Reputation: 45921
I'm desing a database and I have following structure:
Is a game database, and I will have users, games and groups.
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.
I don't know how to know which user has created a game. I thought in two possibilities:
Here are my tables:
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).
How can I represent that a user has created a game?
UPDATE
A game always needs an owner.
Game's owner will be always the same.
When a game is finished can be deleted.
Upvotes: 0
Views: 70
Reputation: 41127
The most common and natural way to represent the ownership relation you've described is to have foreign keys in the Group and Game Tables to the User table.
If you do this you shouldn't need flag fields in the User table signifying whether the user is an owner. This can be determined by a query.
I'd go with
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.
Upvotes: 4
Reputation: 535
Ill say add a userID field to the group and game table which refers to the user that is the owner of the group or game.
Upvotes: 0