Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Say your users can create their own web-based forms (textboxes, selects, etc) and publish them on the web for their users to fill out.

Does anyone have a resource or any advice on how to architect the database to tie into the dynamic forms?

For example, would you create a child table for each form, or different versions of a given form?

share|improve this question

closed as too broad by gnat, Kilian Foth, Yusubov, MichaelT, Dan Pichelman Aug 30 '13 at 15:30

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 3 down vote accepted

Creating new tables dynamically based on user input is usually not a good idea. If the basic structure of forms changes, all the dynamically created tables will need to be updated to include new columns or have old ones removed, and this can cause maintenance headaches. Then there's problem of knowing which table to query (which will probably lead to dynamic SQL which opens up all new problems). And there's probably performance problems too, but I'm not certain how bad that would be. Also, a table is usually used to represent a type of entity (such as "web form") rather than having copies of the same table for each new instance of the same entity.

I'd suggest a single table for the forms. You'll need an identifier on each form to identify whose form it is:

forms
-----
  id (PK)
  name
  owner_id (FK to users.id)
  (other fields)

form_elements
-------------
  id (PK)
  form_id (FK to forms.id)
  element_type_id (FK to element_types.id)
  caption
  (other fields)

element_types
-------------
  id (PK)
  name

element_list_values
-------------------
  id (PK)
  element_id (FK to form_elements.id)
  name
  value
  (other fields??)

Your web application can let users create forms which will be saved in the forms tables, with a reference to the user that created (assuming that you are tracking users as proper entities). The form is populated with form_elements that reference the forms table so they know which form they belong to, and the element_types so they know which type they are. element_types will store a static (mostly) list of different elements a form can have. Types could be: "text_field", "drop_down_list", "radio_buttons", "checkbox". For types such as "drop_down_list" and "radio_buttons", you'll need an extra table, maybe called element_list_values to store the possible options for the lists that these elements normally have.

share|improve this answer
1  
Great solution. TY –  Jeff Borden Jul 8 '13 at 19:09
    
do you know of any existing web form builder GUI tools to use to populate the table schema you outlined above by chance? We're using .NET if relevant. TY. –  Jeff Borden Aug 18 '13 at 14:58
    
@JeffBorden: No, but I'm sure there is something out there. –  FrustratedWithFormsDesigner Aug 20 '13 at 3:24
    
So I'm assuming that the best way to record submitted forms would be with a schema like: form_submissions id (PK) form_id (FK to forms.id) user_id (FK to users.id) ... form_submission_elements id (PK) form_submission_id (FK to form_submissions.id) form_element_id (FK to forms_elements.id) value Look right-ish? –  Jeff Borden Aug 24 '13 at 1:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.