A small but important corner of a database that I’m designing will be used to store the result of arbitrary calculations. These results can be of any type. How can I represent a Value field that can be of any type in a relational database?
The only thing I can think of is to have separate tables based on data type that all have foreign keys back to a generic table. That doesn’t seem right to me because in order to get the values back, I would have to join on a bunch of different tables. There must be a better way.
Your best option is probably going to be to serialize the data and use either string or xml as others are suggesting.
Another option if you are using MS SQL Server is the SQL_VARIANT type, which can store anything except LOBs. I’m not recommending this option over serialization however as serialization work on any platform and is easier to understand and optimize.