Skip to content
Advertisement

Data Model for multiple checkboxes

I currently have a supplier table and now I want to store the regions served by each supplier.

As a first thought, I made my supplier model like this :

Does this seem to be a good pattern? Especially for queries to filter suppliers by region served etc. Is this solution still reliable if in the future I want to add more regions?

Advertisement

Answer

Is this solution still reliable if in the future I want to add more regions?

No. Adding another region entails:

  • adding a new column to the database table
  • amending your application code to reference that new column

Changing the data model and application code in a live application is always a pain point. (Precisely how much pain depends on your change process.)

A better design would be to have a table of REGIONS and an intersection table of SUPPLIER_REGIONS. Build your application around that structure and adding a new region is merely a matter of a some insert statements.


Is it optimal to create an additional table for just 3 values: Europe, Africa and Asia?

Optimality is in the eye of the beholder.

Consider another change: let’s add CUSTOMERS into the mix. We want to know which region our customers belong to. Also we add WAREHOUSES, and want to know in which region they are located. Now we want to know: which is the best Warehouse to store products from Supplier X? Which is the best Warehouse for deliveries to Customer Y? Having a single REGIONS table to join multiple dependent tables makes answering such questions a lot easier than joining flag columns on entity tables.


your proposed solution will make it possible to do operations on the region table like adding more fields (translate name etc)

That’s a good insight. One of the purposes on data modelling is to drive out all the core entities in the domain. Things for which we want to store multiple attributes need to be independent tables.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement