Mysql – I have one table for users, one for a collection of games. My users can own one or many games. How to structure that in the database

database-designMySQL

Since I can't store lists, should I add a column to my table called owned games and then just keep a long string like "game1, game2…"?

Or should I add another table called something like ownership and every row represents the fact that a user is linked to a game…

  ownership

  - gameID
  - owner
  - dateAdded

Best Answer

You first need to decide: can a game be owned by more than one person? (consider also if the answer might possibly be yes in the future).

If yes) Then you have a many-to-many relationship and as you described the only way to do this is to use an intermediary table with atleast these three columns:

  • ID
  • Game_ID (foreign key referencing the primary key in the Game table)
  • Owner_ID (foreign key referencing the primary key in the Owners table)

The naming convention for an intermediary table is normally a combination of the two tables you are connecting. So in your case it would probably be 'Game_Owner'.

If no) You have a one-to-many relationship and no intermediary table is required. In the Game table include a column called 'Owner_ID' which is a foreign key.