Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way to store custom fields in a SQL database?

My friend is building a product to be used by different independent medical units.

The database stores a vast collection of measurements taken at different times, like the temperature, blood pressure, etc...

Let us assume these are held in a table called exams with columns temperature, pressure, etc... (as well as id, patient_id and timestamp). Most of the measurements are stored as floats, but some are of other types (strings, integers...)

While many of these measurements are handled by their product, it needs to allow the different medical units to record and process other custom measurements. A very nifty UI allows the administrator to edit these customs fields, specify their name, type, possible range of values, etc...

He is unsure as to how to store these custom fields.

He is leaning towards a separate table (say a table custom_exam_data with fields like exam_id, custom_field_id, float_value, string_value, ...)

I worry that this will make searching both more difficult to achieve and less efficient.

I am leaning towards modifying the exam table directly (while avoiding conflicts on column names with some scheme like prefixing all custom fields with an underscore or naming them custom_1, ...)

He worries about modifying the database dynamically and having different schemas for each medical unit.

Hopefully some people which more experience can weigh in on this issue.

Notes:

  • he is using Ruby on Rails but I think this question is pretty much framework agnostic, except from the fact that he is only looking for solutions in SQL databases only.

  • I simplified the problem a bit since the custom fields need to be available for more than one table, but I believe this doesn`t really impact the direction to take.

  • (added) A very generic reporting module will need to search, sort, generate stats, etc.. of this data, so it is required that this data be stored in the columns of the appropriate type

  • (added) User inputs will be filtered, for the standard fields as well as for the custom fields. For example, numbers will be checked within a given range (can't have a temperature of -12 or +444), etc... Thus, conversion to the appropriate SQL type is not a problem.

like image 862
Marc-André Lafortune Avatar asked Jan 18 '10 23:01

Marc-André Lafortune


People also ask

What database table are custom fields stored in?

The custom field default value is stored in the genericconfiguration table.

Is SQL the best way to store data?

Anyway, here is the TLDR: Use No-SQL for everything except extremely important data storage and processing like financial transactions. No-SQL is way faster and stores data better.


1 Answers

I've had to deal with this situation many times over the years, and I agree with your initial idea of modifying the DB tables directly, and using dynamic SQL to generate statements.

Creating string UserAttribute or Key/Value columns sounds appealing at first, but it leads to the inner-platform effect where you end up having to re-implement foreign keys, data types, constraints, transactions, validation, sorting, grouping, calculations, et al. inside your RDBMS. You may as well just use flat files and not SQL at all.

SQL Server provides INFORMATION_SCHEMA tables that let you create, query, and modify table schemas at runtime. This has full type checking, constraints, transactions, calculations, and everything you need already built-in, don't reinvent it.

like image 145
Dour High Arch Avatar answered Sep 18 '22 18:09

Dour High Arch