我想存储大量已填写的政府表单,如Application for Federal Assistance。形式多种多样,每年都会发生变化。字段类型各不相同,可以是: boolean、string、date、int等。
存储这些表单以完全规范化数据的最佳方式是什么?
±la:
form
+-----------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| govt_identifier | char(40) | YES | | NULL | |
| description | char(100) | YES | | NULL | |
+-----------------+-----------+------+-----+---------+----------------+
filled_form (a form a person has actually filled out)
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| form_id | int(11) | NO | | NULL | |
| person_id | int(11) | NO | | NULL | |
+-----------+---------+------+-----+---------+----------------+
text_field (a class of input; belongs to a form)
+---------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(40) | YES | | NULL | |
| form_id | int(11) | NO | | NULL | |
+---------+----------+------+-----+---------+----------------+
text_value (a particular input record; belongs to a class and filled_form)
+----------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| value | text | YES | | NULL | |
| text_field_id | int(11) | NO | | NULL | |
| filled_form_id | int(11) | NO | | NULL | |
+----------------+---------+------+-----+---------+----------------+
... continue for all input types发布于 2011-02-14 19:02:17
虽然这是可行的,但您的SQL会有点笨拙,而且非常不直观。您是否考虑过为每个表单单独创建数据模型,然后使用这些数据模型填充表单。它可能看起来更多的前期工作,但您的数据捕获的开发可能会更简单。
发布于 2011-02-14 19:05:14
我会去看看single table inheritance。将每个字段建模为具有子类IntField、BoolField等的基类Field。
Field类将有一个成员Name (string),IntField将有IntValue (int),BoolField将有BoolValue (bit),依此类推。
这要求您在Field-table中为每个可能的类型都有一个列,这是一些空间开销,但另一方面,它为您提供了类型安全。如果您将其建模为单表继承,那么您可能会毫不费力地连接到您最喜欢的ER-mapper。
https://stackoverflow.com/questions/4991292
复制相似问题