0

I have a table that stores general information about a customer (name, address, etc) that is common to all customers. I have a field called CustomerType (list of types) that drives what other fields I need to capture. So if they are a government customer then they will see a different set of custom fields than a non-profit customer would see. I need to create forms that each different CustomerType will be fill out. On the SQL side, I need to figure out the best way to store the data so that when I do reporting it is simple. I don't know the best way to attack this problem.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • [This](http://stackoverflow.com/questions/3579079/how-can-you-represent-inheritance-in-a-database) could probably be of help – Marcus Feb 16 '15 at 15:04

2 Answers2

1

On the SQL side, I need to figure out the best way to store the data so that when I do reporting it is simple. I don't know the best way to attack this problem.

There are many possible approaches each with different strengths and weaknesses, here's some to think about:

  • Create separate customer detail tables for each of the customer types, each containing the fields specific to that customer type. Each detail table keyed on the customer Id. The customer type does not have to be an attribute of the detail table, only of the parent Customer table.

    (+) The correctly normalized solution (although you may find awkward situations where attributes are common to a subset of customer types). The tables will be fairly easy to maintain.

    (-) Reports harder to write - you may find yourself using a LOT of unions or outer joins. Development against this schema is more complex, the extra logic to insert/update attributes in the correct tables for particular customer types must be encoded somewhere. This might become unmanageable if you have many customer types, or if you're adding/changing them frequently.

  • Expand the customer table to contain the super-set of columns required by all customer types, keyed on the customer Id.

    (+) Simple, very easy to report on, simple programming logic.

    (-) The customer-type specific fields are only partially dependent on the key of the customer table (customer Id) - they are really dependent on the combination of customerId/customerType. If there are many extra fields, and if there are few fields common between customer types then this denormalization may result in a very wide table with an unmanageable number of columns. It could be a maintenance nightmare - the table must be modified every time a new customer type is added/change. You might find this a good solution if the number of unique fields required by each customer type is small and they don't change often and if ease of programming and reporting is an overriding concern.

  • Store the customer specific values as name/value pairs in a generic customer Details table, keyed on customerId/customerType/key.

    (+) Very simple to maintain - No data model changes are required to add a new customer type.

    (-) Non-relational, makes pure SQL reporting near impossible and makes integrity constraints very difficult to add. You might see this in specialized use cases e.g. where the data will only ever consumed as JSON and direct reporting will never be a requirement, or in some corporate environments where it may be appealing if database changes are very hard to push through.

justAnotherUser
  • 181
  • 1
  • 6
0

First of all, have a look at some good tutorials on database design and object relational modelling (ORM) A beginner's guide to SQL database design

My personal suggestion for your design would be to create one table to store all costumers, together with some kind of unique customer id and the CustomerType. Next create a separate table for each of the CustomerTypes and for each user that belongs to that type, store that users unique id in a column together with its customertype specific fields.

Lanting
  • 3,060
  • 12
  • 28