问题
I'm trying to build a sort of form builder that'll allow me to define, display and store 'tests' in a flexible way. I.e. Allow the user, through the web interface, to create a new type of test/form ("Grouping") and define a set of fields that will be displayed on the form (any type of field, including date, text, radio, checkbox, etc). I'll also need a results table that'll store the values saved in each form/test.
As an inadequate example, I have the following 3 tables so far:
dd_TestGrouping
- TestGroupingID [pk]
- TestGroupingName "Algebra-1"
- TestGroupingTypeID "Math"
dd_TestFields
- TestFieldID [pk]
- TestGroupingID [fk]
- TestFieldName "Circumference"
- TestFieldType "TextBox"
- Sequence
TestResults
- TestResultID [pk]
- TestFieldID [fk]
- value "50"
- Unit "CM"
The problem with the above - if nothing else - I'm not sure how to dynamically display dropdown lists and linked radio/checkboxes. Also, how can I handle validation?
Thanks in advance for any help/pointers.
回答1:
Sounds like this would be a good case for using an EAV model.
回答2:
To include combo box values you need to extend your model via something like this:
dd_TestFields
- TestFieldID [pk]
- TestGroupingID [fk]
- TestFieldName "Gender"
- TestFieldType "Combo Box"
- Sequence
with a new table:
dd_TestFieldSelection
- TestFieldSelectioniD [pk]
- TestFieldID [fk]
- TestFieldValue "Female"
- Sequence
Validation i think naturally belongs in your dd_TestFields table:
dd_TestFields
- TestFieldID [pk]
- TestGroupingID [fk]
- TestFieldName "Age"
- TestFieldType "Number/Text Box"
- Sequence
- Required "True"
- MinValue "0"
- MaxValue "150"
This is just a rough sketch but you can extend the ideas as you see fit.
回答3:
first, beware of the inner platform effect.
second, supporting dropdowns is just a matter of having a 4th table which includes all the possible values for a "multi" type of field.
回答4:
OK, so in your answers and comments you've described two very different scenarios.
If it's a form builder for algebra tests and described in your sample then please read here. The EAV is very seductive but you will pay a massive price for using a normal RDBMS to construct that model.
However,
If it's health records as you say in a comment. That's a different ballgame. Health records are bizarre because there's essentially a limitless number of symptoms and significant lack of symptoms. Say you have a surgical wound and you spike a fever. That could indicate an infection. If there's no redness, swelling, tenderness or draining puss from the wound, that would be the significant absence of a symptom.
Think of health records like Facebook. When you visit a friend's page, the database has the PK for that person and then gets everything by that. You don't normally do a count of users by hometown and are married for example. That query is a pain against an EAV. Same with health records. Patient 123 shows up, you just need his/her chart. You don't need to query everyone who share 6 symptoms. (BTW, that does happen in research but those EAV records would have to be filtered, pivoted and converted to a more 3nf format.)
If you are doing health records, I would look into some new EAV RDBMS being designed specifically for health records.
来源:https://stackoverflow.com/questions/4247883/db-design-help-eav-form-builder