0

So my scenario demands to store details of businesses which offer varied services. One business can offer n different services. Since each service is different, details pertaining to a service are in individual tables.

table : business 
------------------
business_id, 
(other business specific fields)


table: service_types
----------------------
service_type_id,
service_name,
(other service specific fields)


table: business_services
--------------------------
service_type_id_fk,
business_id_fk,
identifier (id to point the record in respective service table)
(other business service specific fields)


table: service_a (similarly many other services - service_b, service_c etc)
------------------
service_id,
service_type_id_fk,
(other service specific fields)

Now I need to find out

  1. How many services a business provides
  2. Details of each of the service.

For 1, I can simply look up business_services table.

But for 2, with the design I have, I have to store the tables names somewhere; either in server side scripting or in a dedicated table. I can may be store it in service_types table where I store the service name and its table name for further details.

Is it good practice to store table names in a table? Or what could be the best way to address the problem?

NOTE: None of these services are similar and they demand a table for each one. So grouping services into one table is not an option because I want it to be normalized and distribute load as well.

4
  • Can a business offer multiple instances of the same service, i.e. is it "I offer service A and service C" or can there be "I offer service A, service A, service C"? And no, it's not good practice to store table names in the db and use them for logic.
    – Kayaman
    Commented Aug 20, 2019 at 9:17
  • Services unknown or not covered right now will occur and should be addable in the future. It doesn't have to be programmatic though. Commented Aug 20, 2019 at 9:32
  • @Kayaman So there is one registration as such for a business. Rest of all deals with the services and Service A is service A, so I don't think multiple instances makes sense here. In my business_services table for a business_id_fk of 1 and a service_type_id_fk of 1 there will be only one record, no matter what. Commented Aug 20, 2019 at 9:36
  • I wouldn't be doing DDL from client app. I mentioned it needn't be programmatic. The client would inform if they want to include a new service to their list. So that can always be developed without having to make it DDL through client app. Commented Aug 20, 2019 at 9:43

1 Answer 1

2

If you know in advance that you're going to have at most a smallish (under 10) set of services and you don't need to add/remove/change them programmatically or often, I would use separate link tables for the services. Since the client needs to request a service to be added, it's probably a lot simpler to create a new link table and make code changes to reflect the new service than to create a "clever solution" which is overly clever for the amount of changes you're going to have.

With generic solutions you tend to exchange efficiency/simplicity and other things for ease of use (e.g. you have just a single generic link table, but you end up with more complexity because you can't have a foreign key, your queries need to work with your custom solution, you must differentiate the services somehow in your application anyway, etc.).

Even if you were to have a lot of services and they would often change, I wouldn't go for the "dynamic link table" approach. You can definitely make it work somehow, but it would be a hack rather than a good solution. So what would be a good solution in that case? Hard to say from the question alone, but I'm sure it would involve a lot more than just the database.

The gist of the question is "How can I treat different things as if they were the same, except in many cases they need special treatment", and I'm offering the "don't even try if it isn't strictly needed" answer. If that's not to your liking, you'll have to explain a lot more about your system, the services, the data, etc.

One (not saying the best) way to get the amount of services provided by a business would be to use UNION for example. It gives a readable (and easily editable!) query, for when a service is added.

SELECT COUNT(*) FROM (
SELECT 1 FROM business bus JOIN business_services_a a ON (bus.id = a.business_id)
UNION
SELECT 1 FROM business bus JOIN business_services_b b ON (bus.id = b.business_id)
UNION
SELECT 1 FROM business bus JOIN business_services_c c ON (bus.id = b.business_id)
)
4
  • I really don't understand how this answers my question. I'm already following the link / pivot / mapping table concept. If you can't see it, business_services is my link table. You are suggesting to follow the same approach which I'm already following. Let's say I have separate link tables for each service, how is that going to solve the problem? There is no record level relationship that I can establish to resolve using table joins. That again leads to question no 2. How do you suggest to solve it? Commented Aug 21, 2019 at 7:26
  • Please edit the answer with your table design suggestions so that I can clearly understand your suggestion. Commented Aug 21, 2019 at 7:34
  • 1
    @HariHarker I merely suggested you do it the standard way of separate link tables. You won't get any "nice" queries because you'll have to include each link table manually (although if you do this application side with ORM, it should be pretty painless), which is why I said "if you have a small set of services".
    – Kayaman
    Commented Aug 21, 2019 at 7:39
  • 1
    @HariHarker I gave you an example query. If you're looking for something "easy", you're going to make your architecture "way too complex".
    – Kayaman
    Commented Aug 21, 2019 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.