If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Creating a field in a table where a datatype is only known when inserting record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-09, 00:25
DanInAustralia DanInAustralia is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
Creating a field in a table where a datatype is only known when inserting record

Hi,

I am in the process of creating a medical database (Using SQL-Server 2005) which records information about blood samples that arrive for testing at our centre. At times we have to store additional information about the samples that arrive and the patients that these samples were taken from.

These 'parameters' cannot be predicted at design time. Therefore we have created a 'Parameters' table which stores the names of these parameters. Examples of these parameters could be therapy start date (date), viral load (int), height, weight (float).

---Parameters
-ParameterID (int)
-ParameterName (varchar)
-ParameterDataType (varchar)

We have also created a ParameterValue table which stores the value of these parameters. ie:

---ParameterValue
-ParameterValueID
-ParameterID (Foreign key with Parameters.ParameterID)
-Value (varied)

The problem comes from not knowing how to store dates, ints, varchars and floats in the Value field in ParameterValue. Is there any better solution than having a different field to store each of the datatype possibilities?

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-22-09, 06:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
do a search for entity-attribute-value, better known as EAV

this type of design is invariably fraught with whoopsies, complexity, and inefficiency

i don't envy your situation
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-22-09, 07:47
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Other possibilities include an XML column.

I believe medical databases are about the most common example of this type of requirement (never worked on one myself, mind) so that might prove fruitful googling fodder too.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #4 (permalink)  
Old 01-22-09, 20:55
DanInAustralia DanInAustralia is offline
Registered User
 
Join Date: Jan 2009
Posts: 2
Smile

Thanks guys,

we are using an EAV structure in combination with the sql_variant datatype to store values of different types.
Reply With Quote
  #5 (permalink)  
Old 01-23-09, 05:36
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
The problem comes from not knowing how to store dates, ints, varchars and floats in the Value field in ParameterValue. Is there any better solution than having a different field to store each of the datatype possibilities
I haven't worked on medical databases but I have worked on systems similar to what your building. You can simply use a varchar to hold the datatypes you describe (dates, ints, varchars and floats) but I believe you have to convert them explicitly in MS SQL. It's possible you might run into issues where floats converted to varchars can loose some precision but I don't think it will affect you here.

One good idea is to use a procedure to add values to the database - this sproc should check that the "object" exists, that the parameter type exists and that the format of the data is consistent with this data type. You could also add ranges etc so height might have to be within a known min and maximum height. It's also a good idea to have a function to return a given value for an object as this makes the coding far easier.
Code:
select id, name, get_value( id, 'height' )
from my_objects
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On