规范化具有相互关联的m2m关系的数据库



我有一个问卷调查的商业模式。每个问卷都有多个问题和应答者。所以我的模型是这样的:

table questionnaire - id
table respondent - id, name
table question - id, text
table questionnaire_respondent - id, questionnaire_id, respondent_id
table questionnaire_question -  id, questionnaire_id, question_id
table respondent_answer -  id, questionnaire_respondent_id, questionnaire_question_id, answer_text

模型的问题是,在respondent_answer中,我在问卷中有一种数据重复,可以从questionnaire_question_idquestionnaire_respondent_id中计算出来(意思是数据重复)。第二个潜在的问题是,我们可以插入问题问卷与被调查者问卷不对应的行。我知道我可以用插入/更新触发器来保护自己。然而,我正在寻找一种方法来确保在架构层面上做到这一点。因为当前的体系结构似乎不符合5种规范化形式(不确定是哪一种)。如果我没有说清楚问题,请告诉我,我会尽量提供一个例子。

问题是如何规范当前的数据库架构?

有了这个模型,这两个问题都在架构上得到了解决!

table questionnare_name - id, name
table question_text - id, text
table respondent - id, name
table question - id, questionnare_name (FK), question_text (FK)
table answer - id, respondent (FK), question (FK), text
-- User USR exists.
--
user {USR}
PK {USR}
-- Question QUE, worded as QUE_TXT, exists.
--
question {QUE, QUE_TXT}
PK {QUE}
AK {QUE_TXT}
-- Survey (list of questions) SUR exists.
--
survey {SUR}
PK {SUR}
-- Survey SUR contains question QUE.
--
survey_question {SUR, QUE}
PK {SUR, QUE}
FK1 {SUR} REFERENCES survey   {SUR}
FK2 {QUE} REFERENCES question {QUE}
-- User USR provided answer ANS to question QUE
-- of survey SUR.
--
user_survey_question {USR, SUR, QUE, ANS}
PK {USR, SUR, QUE}
FK1 {USR} REFERENCES user {USR}
FK2 {SUR, QUE} REFERENCES 
survey_question {SUR, QUE}

注意:

All attributes (columns) NOT NULL
PK = Primary Key
AK = Alternate Key (Unique)
FK = Foreign Key

最新更新