Many to many relationship?

前端 未结 3 1479
广开言路
广开言路 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-27 03:49

    I would suggest making 1-to-Many relationships instead of making many-to-many relationships. You can accomplish this by having a table that maps between your tblTicket and tblEngineer. For Example:

    tblEngineer
    -----------
    (PK) iEngineerID
    
    
    tblTicket
    ---------
    (PK) iTicketID
    
    
    tblTicketEngineerMap
    --------------------
    (PK) iMapID
    (FK) iEngineerID
    (FK) iTicketID
    

    By doing it this way, an Engineer and a Ticker can be in many maps, making two 1-to-Many relationships, and allowing the functionality you seek.

    Check out this thread as to why you should try to avoid many-to-many table designs.

    0 讨论(0)
  • 2021-01-27 03:50

    Use a column to store different logic values: check this link http://sqlpro.developpez.com/cours/stockageopt/

    enter image description here

    enter image description here

    This means that 3 tables are necessary for data manipulation, including a significant cost to the relational query engine.

    Are there way to simplify this by integrating all these data within the Member table?

    Assign a code to each state:

    Methode 1 : ( use 2 tables )

    tblTicket ( id_Ticket , Engineers )

    Engineers( id_eng , other columns)

    in tblTicket Engineers you can set multiple value like id_1 - id_2 - id_3 ( 1-2-3) 3 engineers

    Methode 2 : ( use 3 tables )

    tblticket ( id_ticket , other columns ) ( primary key : id_Ticket )

    engineers ( id_Eng , other columns) ( primary key : id_Eng )

    TicketEng ( id_Ticket , id_Eng ) ( primary key : (id_Ticket , id_Eng) )

    0 讨论(0)
提交回复
热议问题