views:

91

answers:

3

Im storing columns in database with users able to add and remove columns, with fake columns. How do I implement this efficiently?

+6  A: 

The best way would be to implement the data structure vertically, instead of normal horizontal.

This can be done using something like

TableAttribute
    AttributeID
    AttributeType
    AttributeValue

This application of vertical is mostly used in applications where users can create their own custom forms, and field (if i recall corretly the devexpress form layout allows you to create custom layouts). Mostly used in CRM applications, this is easily modified inproduction, and easily maintainable, but can greatly decrease SQL performance once the data set becomes very large.

EDIT:

This will depend on how far you wish to take it. You can set it up that it will be per form/table, add attributes that describe the actual control (lookup, combo, datetime, etc...) position of the controls, allowed values (min/max/allow null).

This can become a very cumbersome task, but will greatly depend on your actual needs.

astander
I do encourage downvoting accompanied by a comment X-)
astander
For an example of such a structure in the wild, see Trac's "ticket_custom" table.
jemfinch
i was thinking of this, how would i scale later on?
Timmy
I agree with this approach - keep physical and logical concepts seperate; I can't think of any senario where letting users physically add columns is wise (security risk for one). This appraoch will be efficient from a performance perspective but might require a learning curve for you up front, but i'd say it was worth it. Another example here: www.morphological.geek.nz/Morphfolia/Capabilities/CustomProperties.aspx
Adrian K
EAV tables are notoriously bad when it comes to performance. I would not take this aproach.
HLGEM
+1  A: 

I'd think you could allow that at the user-permission level (grant the ALTER privilege on the appropriate tables) and then restrict what types of data can be added/deleted using your presentation layer.

But why add columns? Why not a linked table?

John at CashCommons
+1  A: 

Allowing users to define columns is generally a poor choice as they don't know what they are doing or how to relate it properly to the other data. Sometimes people use the EAV approach to this and let them add as many columns as they want, but this quickly gets out of control and causes performance issues and difficulty in querying the data.

Others take the approach of having a table with user defined columns and give them a set number of columns they can define. This works better performance wise but is more limiting interms of how many new columns they can define.

In any event you should severely restrict who can define the additional columns only to system admins (who can be at the client level). It is a better idea to actually talk to users in the design phase and see what they need. You will find that you can properly design a system that has 90+% of waht the customer needs if you actually talk to them (and not just to managers either, to users at all levels of the organization).

I know it is common in today's world to slough off our responsibility to design by saying we are making things flexible, but I've had to use and provide dba support for many of these systems and the more flexible they try to make the design, the harder it is for the users to use and the more the users hate the system.

HLGEM
thanks, but need this feature. only need to sort fake columns.
Timmy