问题
I want to build an application that allows a user to build its own form(s).
A user can input data into its form and query the data too. The form can be edited after being created and used ( add/remove fields from it ). The form can have small text box, big text box, single list, check box, or a drop down menu.
I've been reading and asking around about which approach is better for building this kind of databases and I got no straight/good answer. I read that in this kind of application, the column based design (and representing each field as a column) is not a good idea since a user can be adding many fields and this is not a good idea, since there should be a limit on the number of columns in a table. However, I can't know in advance how many fields the user will add. (I'm not sure what they mean. It will help a lot if you explain further by giving an example.)
I thought a good idea might be to group the fields according to their types. (type 1: text box , type 2: drop down list, type 3 : single list... and so on). I want to design my tables so that I can upgrade my application and add a new type any time I want without ALTERING my schema. So I was thinking to have 2 tables, one table to hold the metadata about the field: its type, its name, its position maybe in the form and another table to hold the actual value of the field. I can't seem to know what exactly to have in these tables and how to design them correctly. Your help is highly appreciated.
回答1:
**Form** A Form Template
----
formID (PK)
authorID
Name
**FormField** = 1 row for each new question on the form
----------
FormFieldID (PK)
FormID (FK)
FieldID (FK)
SortOrder INT
**FormFieldOptions** - if the form field is a selectbox this will store the options availble to pick from
---------------
FormFieldID (fk)
Name
SortOrder INT
**Field** -- The will be Text Short, Text Long, Checkbox, Radiobutton, Selectbox etc
------
FieldID (PK)
Name
TYPE
That is for creating forms. For saving users answers/responses to the form I will leave for an exersise for the reader ;)
来源:https://stackoverflow.com/questions/4419600/how-to-design-these-two-tables