Many to many relationship?

冷暖自知 提交于 2019-12-24 11:34:25

问题


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 tblEngineer in my MSSQL database.

My application is in C# windows forms, so on the new ticket generation form, I have many textboxes and a comboBox for assigning engineer which is being populated by tblEngineer. Upon ticket generation, all the information entered in this form is stored in tblTicket along with EngineerID from tblEngineer.

It was working great but then my client asked me to add option so that 3 engineers could be assigned on a single ticket.

In the future, I will have to develop a "engineer module" in which engineer will be able to see tickets assigned to him only. It will include a login system for authentication purpose. So if a new ticket is generated and is assigned to 3 engineers, only that 3 engineers should be able to see the ticket and not the others.

How should I go about this approach ? It was really easy if there was only one engineer. Now do I need to make a many-to-many table like tblAssignedEng with ticket id and multiple engineer ids? referenced as a foreign key? I haven't much experience with SQL so I am kinda struggling here and any help would be appreciated.


回答1:


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.




回答2:


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.




回答3:


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

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) )



来源:https://stackoverflow.com/questions/22505938/many-to-many-relationship

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!