Home > other >  Storing multiple ids in a single (SQL) row?
Storing multiple ids in a single (SQL) row?

Time:09-06

So I want to make a webapp that needs to store the following.

"Users" some "Events" and some "Customers"

each "Event" has a "User" which is represented by a userId column.

But each "Event" can have multiple customers, to be able to store all this info, I thought it's better to have a new table "Customers_In_Events" that has 3 columns:

id,     -- Not sure if that's needed
e_id    -- Event ID
c_id    -- Customer ID

I have 2 questions...

  • Is there some better way to do this?

  • If I decide to go with the way I mentioned (an extra table), Would the "id" column be needed?

I am using MySQL by the way.

CodePudding user response:

You can use a bridge model to represent your data, which is also normalized.

  • every event can have multiple customers
  • every customer can have multiple events

so the tabkes would look like

Events    events_coustmers     cutonuers
e_id(PK)  e_id(fk)             c_id(Pk)
e_name    c_id(fk)             c_name
          PK(e_id,c_id)
  • Related