Home > Software design >  How to represent many to many relationship
How to represent many to many relationship

Time:04-03

I have an application and I have a many to many relationship that I can not wrap my mind around how to represent in a database.

I have users, users can create maps, and maps have elements, but when a user creates a map they can add other users to it to give them access to the map.

So... One Map will have many elements Elements will be tied to one map

One user will have many maps Maps will have many users

Each map obviously has an ID column Elements have a column for the Map they belong to

At first I imagined a map table that would have a column for user1ID and a column for user2 ID but now I want to allow as many users are they want.

How would this look in a database?

CodePudding user response:

You would create a maps_users table with columns map_id and user_id, and a primary key of map_id and user_id.
Bonus points if you add a foreign key from map_id to the maps id column in the maps table, and a foreign key from the user_id to the users id column in the users table.

  •  Tags:  
  • sql
  • Related