参考问卷星设计数据库表并给出建表语句
时间: 2023-09-12 16:07:44 浏览: 167
数据库建表语句
根据问卷星的功能和数据结构,可以设计以下数据库表:
1. 用户表(user)
|字段名称 |类型 |说明|
|---|---|---|
|id |int |用户ID,主键自增|
|username |varchar(50) |用户名|
|password |varchar(50) |密码|
|email |varchar(50) |电子邮箱|
|phone |varchar(20) |电话号码|
2. 问卷表(survey)
|字段名称 |类型 |说明|
|---|---|---|
|id |int |问卷ID,主键自增|
|title |varchar(200) |问卷标题|
|description |text |问卷描述|
|start_time |datetime |问卷开始时间|
|end_time |datetime |问卷结束时间|
|status |int |问卷状态,0表示未发布,1表示已发布,2表示已结束|
|user_id |int |创建该问卷的用户ID,外键关联用户表|
3. 问题表(question)
|字段名称 |类型 |说明|
|---|---|---|
|id |int |问题ID,主键自增|
|survey_id |int |所属问卷ID,外键关联问卷表|
|title |varchar(200) |问题标题|
|description |text |问题描述|
|type |int |问题类型,1表示单选题,2表示多选题,3表示填空题,4表示问答题|
|is_required |tinyint |是否必填,0表示否,1表示是|
4. 选项表(option)
|字段名称 |类型 |说明|
|---|---|---|
|id |int |选项ID,主键自增|
|question_id |int |所属问题ID,外键关联问题表|
|content |varchar(200) |选项内容|
5. 回答表(answer)
|字段名称 |类型 |说明|
|---|---|---|
|id |int |回答ID,主键自增|
|question_id |int |所回答的问题ID,外键关联问题表|
|content |text |回答内容|
|user_id |int |回答用户的ID,外键关联用户表|
|survey_id |int |所属问卷ID,外键关联问卷表|
根据以上设计,可以给出以下建表语句:
```
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `survey` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
`description` text NOT NULL,
`start_time` datetime NOT NULL,
`end_time` datetime NOT NULL,
`status` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRAINT `survey_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `question` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`survey_id` int(11) NOT NULL,
`title` varchar(200) NOT NULL,
`description` text NOT NULL,
`type` int(11) NOT NULL,
`is_required` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
KEY `survey_id` (`survey_id`),
CONSTRAINT `question_ibfk_1` FOREIGN KEY (`survey_id`) REFERENCES `survey` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `option` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`content` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
KEY `question_id` (`question_id`),
CONSTRAINT `option_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `question` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `answer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`content` text NOT NULL,
`user_id` int(11) NOT NULL,
`survey_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `question_id` (`question_id`),
KEY `user_id` (`user_id`),
KEY `survey_id` (`survey_id`),
CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `question` (`id`),
CONSTRAINT `answer_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
CONSTRAINT `answer_ibfk_3` FOREIGN KEY (`survey_id`) REFERENCES `survey` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
阅读全文