-- 创建数据库 (如果不存在)
CREATE DATABASE IF NOT EXISTS `exam` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE `exam`;

-- 1. 题库表 (question)
CREATE TABLE IF NOT EXISTS `question` (
    `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '题目ID',
    `title` TEXT NOT NULL COMMENT '题目内容',
    `type` INT NOT NULL COMMENT '题目类型: 1-选择题, 2-简答题',
    `answer` TEXT COMMENT '学生答案参考(仅简答题)',
    `answer_a` VARCHAR(255) COMMENT '选项A(仅选择题)',
    `answer_b` VARCHAR(255) COMMENT '选项B(仅选择题)',
    `answer_c` VARCHAR(255) COMMENT '选项C(仅选择题)',
    `answer_d` VARCHAR(255) COMMENT '选项D(仅选择题)',
    `r_answer` TEXT COMMENT '正确答案',
    `score` INT NOT NULL DEFAULT 0 COMMENT '题目分值'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='题库表';

-- 2. 试卷表 (exam_paper)
CREATE TABLE IF NOT EXISTS `exam_paper` (
    `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '试卷自增ID',
    `uid` VARCHAR(64) NOT NULL UNIQUE COMMENT '试卷唯一标识(UUID等)',
    `title` VARCHAR(255) COMMENT '试卷标题',
    `select` TEXT COMMENT '选择题集合或相关配置',
    `content` TEXT COMMENT '简答题集合或相关内容'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='生成的试卷表';

-- 3. 答题记录表/批改记录表 (exam_back)
CREATE TABLE IF NOT EXISTS `exam_back` (
    `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '答题记录自增ID',
    `uid` VARCHAR(64) NOT NULL UNIQUE COMMENT '答题记录唯一标识',
    `examId` VARCHAR(64) NOT NULL COMMENT '关联的试卷UID',
    `title` VARCHAR(255) COMMENT '试卷标题(冗余防丢)',
    `name` VARCHAR(100) NOT NULL COMMENT '做题人姓名',
    `content` JSON COMMENT '包含题目和所填答案的JSON数组',
    `score` VARCHAR(50) COMMENT '总得分/批改状态'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='学生答卷与批改表';