问卷调查数据库设计

设计一个问卷调查数据库时,需要考虑以下几个关键要素:问卷、问题、选项、用户回答等。以下是一个基本的数据库设计方案,使用关系型数据库(如MySQL、PostgreSQL)来实现。

1. 数据库表设计

1.1 问卷表 (surveys)

存储问卷的基本信息。

字段名 数据类型 描述
survey_id INT (主键) 问卷的唯一标识
title VARCHAR(255) 问卷标题
description TEXT 问卷描述
created_at DATETIME 创建时间
updated_at DATETIME 更新时间

1.2 问题表 (questions)

存储问卷中的问题。

字段名 数据类型 描述
question_id INT (主键) 问题的唯一标识
survey_id INT (外键) 关联的问卷ID
question_text TEXT 问题内容
question_type VARCHAR(50) 问题类型(单选、多选、文本等)
is_required BOOLEAN 是否必答
order INT 问题顺序

1.3 选项表 (options)

存储问题的选项(适用于单选、多选等问题类型)。

字段名 数据类型 描述
option_id INT (主键) 选项的唯一标识
question_id INT (外键) 关联的问题ID
option_text VARCHAR(255) 选项内容
order INT 选项顺序

1.4 用户回答表 (responses)

存储用户对问卷的回答。

字段名 数据类型 描述
response_id INT (主键) 回答的唯一标识
survey_id INT (外键) 关联的问卷ID
user_id INT (外键) 用户ID(如果有用户系统)
created_at DATETIME 回答时间

1.5 用户回答详情表 (response_details)

存储用户对每个问题的具体回答。

字段名 数据类型 描述
detail_id INT (主键) 回答详情的唯一标识
response_id INT (外键) 关联的回答ID
question_id INT (外键) 关联的问题ID
option_id INT (外键) 关联的选项ID(如果是选择题)
answer_text TEXT 用户回答的文本内容(如果是文本题)

2. 表之间的关系

  • surveys 表与 questions 表是一对多的关系,一个问卷可以有多个问题。
  • questions 表与 options 表是一对多的关系,一个问题可以有多个选项。
  • responses 表与 response_details 表是一对多的关系,一个回答可以包含多个问题的回答详情。
  • responses 表与 surveys 表是多对一的关系,一个问卷可以有多个回答。
  • response_details 表与 questions 表是多对一的关系,一个问题可以有多个回答详情。

3. 示例SQL语句

3.1 创建表

CREATE TABLE surveys (
    survey_id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE questions (
    question_id INT AUTO_INCREMENT PRIMARY KEY,
    survey_id INT,
    question_text TEXT NOT NULL,
    question_type VARCHAR(50) NOT NULL,
    is_required BOOLEAN DEFAULT TRUE,
    order INT,
    FOREIGN KEY (survey_id) REFERENCES surveys(survey_id) ON DELETE CASCADE
);

CREATE TABLE options (
    option_id INT AUTO_INCREMENT PRIMARY KEY,
    question_id INT,
    option_text VARCHAR(255) NOT NULL,
    order INT,
    FOREIGN KEY (question_id) REFERENCES questions(question_id) ON DELETE CASCADE
);

CREATE TABLE responses (
    response_id INT AUTO_INCREMENT PRIMARY KEY,
    survey_id INT,
    user_id INT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (survey_id) REFERENCES surveys(survey_id) ON DELETE CASCADE
);

CREATE TABLE response_details (
    detail_id INT AUTO_INCREMENT PRIMARY KEY,
    response_id INT,
    question_id INT,
    option_id INT,
    answer_text TEXT,
    FOREIGN KEY (response_id) REFERENCES responses(response_id) ON DELETE CASCADE,
    FOREIGN KEY (question_id) REFERENCES questions(question_id) ON DELETE CASCADE,
    FOREIGN KEY (option_id) REFERENCES options(option_id) ON DELETE CASCADE
);

3.2 插入数据

-- 插入问卷
INSERT INTO surveys (title, description) VALUES ('Customer Satisfaction Survey', 'This survey aims to gather feedback from our customers.');

-- 插入问题
INSERT INTO questions (survey_id, question_text, question_type, is_required, order) VALUES (1, 'How satisfied are you with our product?', 'single_choice', TRUE, 1);

-- 插入选项
INSERT INTO options (question_id, option_text, order) VALUES (1, 'Very Satisfied', 1);
INSERT INTO options (question_id, option_text, order) VALUES (1, 'Satisfied', 2);
INSERT INTO options (question_id, option_text, order) VALUES (1, 'Neutral', 3);
INSERT INTO options (question_id, option_text, order) VALUES (1, 'Dissatisfied', 4);
INSERT INTO options (question_id, option_text, order) VALUES (1, 'Very Dissatisfied', 5);

-- 插入用户回答
INSERT INTO responses (survey_id, user_id) VALUES (1, 123);

-- 插入用户回答详情
INSERT INTO response_details (response_id, question_id, option_id) VALUES (1, 1, 2);

4. 扩展考虑

  • 用户系统:如果有用户系统,可以在 responses 表中加入 user_id 字段,关联到用户表。
  • 匿名回答:如果允许匿名回答,user_id 可以为空。
  • 多语言支持:如果需要支持多语言,可以在 questionsoptions 表中加入语言字段。
  • 数据分析:可以通过SQL查询或BI工具对回答数据进行分析,生成报表。

5. 总结

这个设计方案是一个基础的问卷调查数据库设计,可以根据具体需求进行扩展和优化。例如,增加问卷的状态(草稿、发布、关闭)、问题的逻辑跳转、用户权限管理等。

posted @ 2025-09-08 16:40  VipSoft  阅读(56)  评论(0)    收藏  举报