Best way to do this database design

Discussion in 'MySQL' started by xionhack, Dec 8, 2013.

  1. #1
    Hello. In my business logic, I have a user, a company (users can be part of the company) and products.

    A product can be owned by a user or by a company, the company can assign it to a user later, but it would still be owned by the company (in case the company fires the user).

    My thought is to have the user table, the company table and the product table.

    I'm thinking about having a product_owner table, where I would have product_id, user_id, company_id, agent_assigned.

    If the product is owned by a user then only product_id and user_id will be filled. If it's owned by a company, then product_id, company_id and agent_assigned would be filled.

    Is this the best way to do it? It doesn't seem good to me.
     
    xionhack, Dec 8, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,830
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I'd probably set up the product_owner table as

    id
    product_id
    foreign_id
    foreign_type (user, company, agent)
    status (active, inactive to retain the history of ownership)

    then you query the product_owner and you know based on the type what you are looking at and where to go

    agent is the only possibly tricky one where you'd have the second step to query the company.
     
    sarahk, Dec 8, 2013 IP
  3. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #3
    why having both user & company table? you can have a single table and a column estabilishing user type (user/company enum)
     
    crivion, Dec 9, 2013 IP
    sarahk likes this.