Click here to Skip to main content
16,015,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I build Project that give the client the power to add new section,new field in form,
Need to be able to dynamically add User-Defined fields of any data type Like "date", "boolean" or "text"

There are many Options :

1) Create a big table with StringValue1, StringValue2... IntValue1, IntValue2,... etc. I hate this idea, but will consider it if someone can tell me it is better than other ideas and why.

2) Create a dynamic table which adds a new column on demand as needed. I also don't like this idea since I feel performance would be slow unless you indexed every column.

3) Something else?

Any Help
Posted
Updated 7-Oct-13 0:12am
v5

1 solution

not sure exactely what you mean ...

On the rare occasion I have had to design a table, that may require extra columns later on, I usually do something like add :-

user-field-1-text-value text
..
user-field-5-text-value text
user-field-1-int-value int
..
user-field-5-int-value int

for the values (and allow null values), then have a corresponding text field for each user field that holds 'what the value represents', so ..

user-field-1-text-description text
..
user-field-1-int-description text
..

for say SQLite - if you think you might need user date fields, then add a number of those

I have seen commercial products that do the same sort of thing, ultimately I think 'typing' the fields is better that forcing converts, but Im not sure its a great way of doing it ...

[Edit : it really, really, depends on your circumstances/requirements, and how much flexibility you need - but it occurs to be you could define a 'blob' (or clob) type field, and do the same sort of thing with/by storing JSON in that field - means a lot more work though /Edit]
 
Share this answer
 
v2
Comments
Ahmad Al Halabi 6-Oct-13 16:07pm    
I improved my question Garth
Garth J Lancaster 6-Oct-13 19:59pm    
Well, it doesnt change my response - my 'user-field-1-text-value text' is equivalent to your StringValue1 for instance - while it may appear that these extra fields are redundant, they are simple, AS LONG AS, you catalog what they are used for - hence the matching decription fields - call them what you want, UDF-String-1, UDF-String-1-Desc, it doesnt matter - its the idea. So, points for simplicity

Not sure on dynamic tables

I Really like my second idea, its ultimately flexible, but, needs other routines to manage it - ie serialise to/from JSON stored in a blob/clob, and of course, you'd document the JSON etc

good luck
'g'
Ahmad Al Halabi 7-Oct-13 6:11am    
i don't think this is the best solution what if the client add 10 or 20 new field,than i must add 20 field in the SQl !!
and what if client want to add a "boolean field" or "date field" ?!
Garth J Lancaster 7-Oct-13 6:27am    
define 'best solution' :-)

if you cant nail down requirements to a fixed table definition, then, you're going to have to pick something you can live with or update your schema every time a field needs to be added - at least with the redundant fields approach you can minimise that

you havnt said anything about the JSON in a blob/clob approach

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900