1

我正在为如何为有人(非匿名)填写问卷的情况建立数据库而苦苦挣扎。人与问卷的关系是多对多的——一个人完成多份问卷,一份问卷由多人完成。所以,我有一个 PERSON 表、一个 QUESTIONNAIRE 表和一个连接表 (PERSON-QUESTIONNAIRE)。

但是我应该把问卷中出现的信息放在哪里呢?问卷项目有两个部分(两个字段),一个带有人们将回答的问题或陈述(ITEM),另一个带有用于回答的空间(RESPONSE)。假设所有这些都放在一个单独的表中,该表与什么连接?(QUESTIONNAIRE 表……一个 QUESTIONNAIRE 可以有多个 QUESTIONNAIRE-ITEMS?)

而且,如果我对最后一点是正确的,我一生都无法弄清楚如何设置一个数据库网格(在 Delphi 中),它将显示选定问卷和选定人员的 ITEM 和 RESPONSE 字段。(我知道如何用 db-aware 组件编写 master/detail 关系,但这就像两个 master 的细节。)

4

2 回答 2

7

You will have a table QUESTIONNAIRE-ITEMS (the questions) and a separate table QUESTIONNAIRE-RESPONSES (the answers), because there will be multiple answers for a single question, those from multiple persons. The former gets a foreign key to QUESTIONNAIRE, while the latter gets foreign keys to both QUESTIONNAIRE-ITEMS and PERSON.

In fact, the PERSON-QUESTIONNAIRE table could be omitted, since that information could be queried from the other tables. So for the design I suggest:

  • Surveys: ID (PK), Description, etc...
  • Users: ID (PK), Name, etc...
  • Questions: ID (PK), SurveyID (FK), Question, etc...
  • Answers: ID (PK), QuestionID (FK), UserID (FK), Answer, etc...

The view for the DBGrid will be based on a query like:

SELECT 
  Question, 
  Answer,
  ...
FROM 
  Questions INNER JOIN Answers ON Questions.ID = Answers.QuestionID
WHERE
  Questions.SurveyID = :SurveyID AND Answers.UserID = :UserID

In another part of you GUI, you select the SurveyID and UserID parameters.

And as a bonus: the query to acquire surveys and users, as an alternative to your separate join table, will look like :

SELECT
  Surveys.ID,
  Users.ID
FROM
  Users INNER JOIN (
    Answers INNER JOIN (
      Questions INNER JOIN (
        Surveys
      ) ON Questions.SurveyID = Surveys.ID
    ) ON Answers.QuestionID = Questions.ID
  ) ON Users.ID = Answers.UserID
GROUP BY
  Surveys.ID,
  Users.ID
于 2012-06-28T19:52:51.933 回答
3

ITEM 和 RESPONSE 应该是不同的表;它们之间存在一对多的关系。(一个项目属于一个问卷,但很多人可以提供对该项目的响应,因此每个项目将有多个响应。)

我推荐这样的设置:

PERSON  <---- PERSON_QUESTIONNAIRE ----> QUESTIONNAIRE
                        ^                       ^
                        |                       |
                        |                       |
                     RESPONSE      ----->     ITEM

我对如何设置 DB 网格(不是 Delphi 大师)没有任何建议,但有时解决设计问题将有助于技术解决方案变得更清晰。

于 2012-06-28T19:54:31.407 回答