Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Pattern to add columns in database table dynamically

Tags:

sql

database

The user wants to add new fields in UI dynamically. This new field should get stored in database and they should be allowed to perform CRUD on it.

Now I can do this by specifying a XML but I wanted a better way where these new columns are searchable. Also the idea of firing ALTER statement and adding a new column seems wrong.

Can anyone help me with a design pattern on database server side of how to solve this problem?

like image 967
Rohit Harshvardhan Avatar asked Dec 20 '10 19:12

Rohit Harshvardhan


2 Answers

This can be approached using a key value system. You create a table with the primary key column(s) of the table you want to annotate, a column for the name of the attribute, and a column for its value. When you user wants to add an attribute (say height) to the record of person 123 you add a row to the new table with the values (123, 'HEIGHT', '140.5').

In general you cast the values to TEXT for storage but if you know all the attributes will be numeric you can choose a different type for the value column. You can also (not recommended) use several different value columns depending on the type of the data.

This technique has the advantage that you don't need to modify the database structure to add new attributes and attributes are only stored for those records that have them. The disadvantage is that querying is not as straightforward as if the columns were all in the main data table.

like image 58
Larry Lustig Avatar answered Oct 05 '22 11:10

Larry Lustig


There is no reason why a qualified business user should not be allowed to add a column to a table. It is less likely to cause a problem than just about anything else you can imagine including adding a new row to a table or changing. the value of a data element.

Using either of the methods described above do not avoid any risk; they are simply throwbacks to COBOL filler fields or unnecessary embellishments of the database function. The result can still be unnormalized and inaccurate.

These same business persons add columns to spreadsheets and tables to Word documents without DBAs getting in their way.

Of course, just adding the column is the smallest part of getting an information system to work, but it is often the case that it is perceived to be an almost insurmountable barrier. It is in fact 5 min worth of work assuming you know where to put it. Adding a column to the proper table with the proper datatype is easy to do, easy to use, and has the best chance of encouraging data quality.

like image 21
Bob Schmidt Avatar answered Oct 05 '22 12:10

Bob Schmidt