views:

380

answers:

9

I am creating a laboratory database which analyzes a variety of samples from a variety of locations. Some locations want their own reference number (or other attributes) kept with the sample.

How should I represent the columns which only apply to a subset of my samples?

Option 1: Create a separate table for each unique set of attributes?
SAMPLE_BOILER: sample_id (FK), tank_number, boiler_temp, lot_number
SAMPLE_ACID: sample_id (FK), vial_number
This option seems too tedious, especially as the system grows.

Option 1a: Class table inheritance (link): Tree with common fields in internal node/table
Option 1b: Concrete table inheritance (link): Tree with common fields in leaf node/table

Option 2: Put every attribute which applies to any sample into the SAMPLE table.
Most columns of each entry would most likely be NULL, however all of the fields are stored together.

Option 3: Create _VALUE_ tables for each Oracle data type used.
This option is far more complex. Getting all of the attributes for a sample requires accessing all of the tables below. However, the system can expand dynamically without separate tables for each new sample type.

SAMPLE:
 sample_id*
 sample_template_id (FK)

SAMPLE_TEMPLATE:
 sample_template_id*
 version *
 status
 date_created
 name

SAMPLE_ATTR_OF     
 sample_template_id* (FK)
 sample_attribute_id* (FK)

SAMPLE_ATTRIBUTE:
 sample_attribute_id*
 name
 description

SAMPLE_NUMBER:
 sample_id* (FK)
 sample_attribute_id (FK)
 value

SAMPLE_DATE:
 sample_id* (FK)
 sample_attribute_id (FK)
 value

Option 4: (Add your own option)

A: 

I would base your decision on the how you usually see the data. For instance, if you get 5-6 new attributes per day, you're never going to be able to keep up adding new columns. In this case you should create columns for 'standard' attributes and add a key/value layout similar to your 'Option 3'.

If you don't expect to see this, I'd go with Option 1 for now, and modify your design to 'Option 3' only if you get to the point that it is turning into too much work. It could end up that you have 25 attributes added in the first few weeks and then nothing for several months. In which case you'll be glad you didn't do the extra work.

As for Option 2, I generally advise against this as Null in a relational database means the value is 'Unknown', not that it 'doesn't apply' to a specific record. Though I have disagreed on this in the past with people I generally respect, so I wouldn't start any wars over it.

Mark Roddy
A: 
David
I don't like userdata fields. Most userdata# entries would most likely be NULL. Also, it would be very difficult to remember what userdata2 is for each type of sample.
Steven
A: 

If the set of sample attributes was relatively static then the pragmatic solution that would make your life easier in the long run would be option #2 - these are all attributes of a SAMPLE so they should all be in the same table.

Ok - you could put together a nice object hierarchy of base attributes with various extensions but it would be more trouble than it's worth. Keep it simple. You could always put together a few views of subsets of sample attributes.

I would only go for a variant of your option #3 if the list of sample attributes was very dynamic and you needed your users to be able to create their own fields.

In terms of implementing dynamic user-defined fields then you might first like to read through Tom Kyte's comments to this question. Now, Tom can be pretty insistent in his views but I take from his comments that you have to be very sure that you really need the flexibility for your users to add fields on the fly before you go about doing it. If you really need to do it, then don't create a table for each data type - that's going too far - just store everything in a varchar2 in a standard way and flag each attribute with an appropriate data type.

create table sample (
    sample_id integer,
    name varchar2(120 char),
    constraint pk_sample primary key (sample_id)
);

create table attribute (
    attribute_id integer,
    name varchar2(120 char) not null,
    data_type varchar2(30 char) not null,
    constraint pk_attribute primary key (attribute_id)
);

create table sample_attribute (
    sample_id integer,
    attribute_id integer,
    value varchar2(4000 char),
    constraint pk_sample_attribute primary key (sample_id, attribute_id)
);

Now... that just looks evil doesn't it? Do you really want to go there?

Nick Pierpoint
The set of sample attributes (once setup will be relatively static). However, I anticipate the number of different sample types (or templates) to be in the hundreds each with its own set of attributes. Also, I anticipate over 50,000 samples per year.Option 1 would be way too tedious to maintain.Option 2 clearly violates normalization. Also, as the database grows and columns are added, nearly all of the "sometimes used" columns will be NULL.Option 3 seems reasonable, except for efficiency.
Steven
Normalization is great in a classroom - but if you want to make something work you sometimes need to be pragmatic and have a partially de-normalized view. "Option 3 seems reasonable, except for efficiency" - efficiency is everything. The end user will not care a tot whether most of the columns in SAMPLE are null - he/she will care a lot if the application is inefficient. Future developers will also care a lot if they have to put together reports on these user-defined fields.
Nick Pierpoint
+3  A: 

To help with Googling, your third option looks a little like the Entity-Attribute-Value pattern, which has been discussed on StackOverflow before although often critically.

As others have suggested, if at all possible (eg: once the system is up and running, few new attributes will appear), you should use your relational database in a conventional manner with tables as types and columns as attributes - your option 1. The initial setup pain will be worth it later as your database gets to work the way it was designed to.

Another thing to consider: are you tied to Oracle? If not, there are non-relational databases out there like CouchDB that aren't constrained by up-front schemas in the same way as relational databases are.


Edit: you've asked about handling new attributes under option 1 (now 1a and 1b in the question)...

  • If option 1 is a suitable solution, there are sufficiently few new attributes that the overhead of altering the database schema to accommodate them is acceptable, so...

  • you'll be writing database scripts to alter tables and add columns, so the provision of a default value can be handled easily in these scripts.

Of the two 1 options (1a, 1b), my personal preference would be concrete table inheritance (1b):

  • It's the simplest thing that works;
  • It requires fewer joins for any given query;
  • Updates are simpler as you only write to one table (no FK relationship to maintain).

Although either of these first options is a better solution than the others, and there's nothing wrong with the class table inheritance method if that's what you'd prefer.

It all comes down to how often genuinely new attributes will appear.

  • If the answer is "rarely" then the occasional schema update can cope.

  • If the answer is "a lot" then the relational DB model (which has fixed schemas baked-in) isn't the best tool for the job, so solutions that incorporate it (entity-attribute-value, XML columns and so on) will always seem a little laboured.

Good luck, and let us know how you solve this problem - it's a common issue that people run into.

Dan Vinton
With Option 1, how should I handle adding/removing attributes while avoiding NULL values?
Steven
@Steven - see the updated answer (600 characters isn't enough...)
Dan Vinton
A: 

This might be a dumb question but what do you need to do with the attribute values? If you only need to display the data then just store them in one field, perhaps in XML or some serialised format.

You could always use a template table to define a sample 'type' and the available fields you display for the purposes of a data entry form.

If you need to filter on them, the only efficient model is option 2. As everyone else is saying the entity-attribute-value style of option 3 is somewhat mental and no real fun to work with. I've tried it myself in the past and once implemented I wished I hadn't bothered.

Try to design your database around how your users need to interact with it (and thus how you need to query it), rather than just modelling the data.

simonrjones
*Designer:* "will you ever want to filter on this vial number?" *User:* "no, never" **(1 year later...)** *User:* "why can't we search for a vial number?"
Jeffrey Kemp
+1  A: 

Option 1, except that it's not a separate table for each set of attributes: create a separate table for each sample source.

i.e. from your examples: samples from a boiler will have tank number, boiler temp, lot number; acid samples have vial number.

You say this is tedious; but I suggest that the more work you put into gathering and encoding the meaning of the data now will pay off huge dividends later - you'll save in the long term because your reports will be easier to write, understand and maintain. Those guys from the boiler room will ask "we need to know the total of X for tank grouped by this set of boiler temperature ranges" and you'll say "no prob, give me half an hour" because you've done the hard yards already.

Option 2 would be my fall-back option if Option 1 turns out to be overkill. You'll still want to analyse what fields are needed, what their datatypes and constraints are.

Option 4 is to use a combination of options 1 and 2. You may find some attributes are shared among a lot of sample types, and it might make sense for these attributes to live in the main sample table; whereas other attributes will be very specific to certain sample types.

Jeffrey Kemp
With Option 1 (or your 4), what would be the best method for adding or removing attributes for each "source"? Adding a column to a table, introduces either a NULL value or an implicit value for all previous entries. No longer storing a column would mean NULL values for all future entries. Creating a new table for each "version" is horrible for searching for a common attribute throughout the old tables.
Steven
@Steven: the problem of how to create data for new attributes in a model for existing data is orthogonal to the problem of how to structure it. Either way you have to either (a) leave it NULL and interpret this as "unknown", or (b) set it to some default value if it makes sense. Whatever option you choose will determine how visible this problem is, but it won't solve it for you.
Jeffrey Kemp
+1  A: 

You should really go with Option 1. Although it is more tedious to create, Option 2 and 3 will bite you back when trying to query you data. The queries will become more complex.

In fact, the most important part of storing the data, is querying it. You haven't mentioned how you are planning to use the data, and this is a big factor in the database design. As far as I can see, the first option will be most easy to query. If you plan on using reporting tools or an ORM, they will prefer it as well, so you are keeping your options open.

In fact, if you find building the tables tedious, try using an ORM from the start. Good ORMs will help you with creating the tables from the get-go.

OmerGertel
A: 

I work on both a commercial and a home-made system where users have the ability to create their own fields/controls dynamically. This is a simplified version of how it works.

Tables:

  • Pages
    • Controls
      • Values

A page is just a container for one or more controls. It can be given a name.

Controls are linked to pages and represents user input controls. A control contains what datatype it is (int, string etc) and how it should be represented to the user (textbox, dropdown, checkboxes etc).

Values are the actual data that the users have typed into the controls, a value contains one column for every datatype that it can represent (int, string, etc) and depending on the control type, the relevant column is set with the user input.

There is an additional column in Values which specifies which group the value belong to. Each time a user fills in a form of controls and clicks save, the values typed into the controls are saved into the same group so that we know that they belong together (incremental counter).

CodeSpeaker
A: 

CodeSpeaker, I like you answer, it's pointing me in the right direction for a similar problem. But how would you handle drop-downlist values? I am thinking of a Lookup table of values so that many lookups link to one UserDefinedField. But I also have another problem to add to the mix. Each field must have multiple linked languages so each value must link to the equivilant value for multiple languages. Maybe I'm thinking too hard about this as I've got about 6 tables so far.

Richard