Many to many relationship?

前端 未结 3 1484
广开言路
广开言路 2021-01-27 03:09

Guys I am trying to make a simple ticket generation system for my company as a favor. For now, I have a table called tblTicket and another table called tblEng

3条回答
  •  -上瘾入骨i
    2021-01-27 03:37

    Standard practice would be this, as an example...

    You have a "tblEngineer" table...

     tblEngineer
     -----------
     (PK) EngineerId
     EngineerName
    

    And a "tblTicket" table...

     tblTicket
     ---------
     (PK) TicketId
     TicketDetails
    

    You now add a link table called "tblEngineerTickets" (or similar) which references the Ids of both the Engineer and their tickets...

     tblEngineerTickets
     ------------------
     (PK) EngineerTicketId
     (FK) EngineerId
     (FK) TicketId
    

    So that way, you keep all the Ticket Details and the Engineer details separately, and link them using ONLY the Ids... the link table would look something like this...

      EngineerId | TicketId
     ------------+----------
          1      |    1
          1      |    2
          1      |    3
          2      |    1
          2      |    2
          2      |    3
    

    This way, you can have multiple engineers assigned to one ticket, and/or multiple tickets assigned to an engineer.

    This is best practice and it gives you the most opportunity for expansion. If you were to just add fields to your existing Engineer tables saying "Ticket1", "Ticket2", "Ticket3" etc... you would be effectively be placing a limit on the code, and potentially you'd have to keep going in to the code to add columns.

提交回复
热议问题