I have come across a need of storing runtime determined values in a SQL database.
For example, there is a GUI where a user can add new editable fields. So the user adds a field Name
, chooses the value type String
and fills in the value Arthur
.
Then the user adds a new field Money
, chooses the value type Decimal
and fills in the value 10.05
.
As seen above, the user is able to add any fields with any value types.
My idea is to have a SQL table schema as follow:
- FieldName (varchar) - eg.
'Money'
or'IsAlive'
- FieldType (varchar) - eg
'Decimal'
or'Boolean'
- FieldValue (varchar) - eg.
'10.5'
or'1'
This would mean that I can store any pre-defined data type in the table whether it's a string
, boolean
or long
.
The problem is that I am not sure it's the best approach to cast those values to string
and then cast again when reading from the table.
Another option would be to have a column of each type but that sounds even worse.
Does my approach to cast everything to string sounds sensible or should I try something else?
ALTER TABLE
statements (with certain limits, of course) and add fields to the actual table.