38

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?

0

1 Answer 1

47

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.

8
  • 2
    Great solution. TY Commented Jul 8, 2013 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. Commented Aug 18, 2013 at 14:58
  • @JeffBorden: No, but I'm sure there is something out there. Commented Aug 20, 2013 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? Commented Aug 24, 2013 at 1:09
  • @FrustratedWithFormsDesigner How would an select to generate table from the form fields and the fields values in this schema!? Commented Aug 16, 2015 at 20:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.