What is the cardinality between a request and the services included in it?

14

I have class Solicitacao and class Servicos . After insertion into the database I want to retrieve in a query all the services associated with that request.

For example, on one screen I insert the request and several services. When the time comes to make the query I want to bring the request and once I click on a button a dialog appears with all the services of that request.

Would this relationship be 1-N or N-N ?

My diagram:

    
asked by anonymous 14.08.2015 / 16:18

1 answer

11

It depends on what these services are.

If the services are specific to this request, that is, they are rows of items that the request must have, then it is 1-N . For each request you have several services. But a service can only be in a request.

But if the services are generic and are registered in the request directly, then you have a N-M relationship ( N-N would mean that the relation should have a symmetric number of items). You have a request that is made up of several services. Each service can be in as many requests as needed.

So by the description is the second form. But in general this is bad design. The right thing would be to have a table of Itens de solicitação in relation 1-N . And each item has a relation with Serviços of N-1 . That is, each request item has a related service, but each service can be in any number of request items.

By editing the question and placing the diagram, it appears that the Servico table functions as a request item, so it would be 1-N . The name gives wrong indication of what the table is. The codigo column is also weird. Is this service code? And how does it link with the request? If this is the request code, it has a link, but then it would have to be 1-1 since it would not be possible to have a primary key in any other way.

Modeling depends a lot on the specific situation. It has some basic ideas to follow but without fully understanding the problem is difficult to say something. And from what I see one of the biggest difficulties for developers is to fully understand the problem. When a detail escapes and models wrong, it will pay up front. I still made a mistake, even with more than 30 years of experience. But I make a big effort to get it right because this is more important than most other skills in our work. Understanding the wrong problem will surely produce the wrong solution and it is not always obvious until it is too late and it is much more difficult to fix the problem.

One more detail: you are using double for monetary values, this is terrible. this is terrible . The use of long for the codes does not seem appropriate to me.

    
14.08.2015 / 16:29