166 lines
7.8 KiB
SQL
166 lines
7.8 KiB
SQL
-- 创建数据库初始表结构
|
||
-- 版本: V1
|
||
-- 描述: 创建用户、会话、消息、收藏、文件等核心业务表
|
||
|
||
-- 设置字符集
|
||
SET NAMES utf8mb4;
|
||
SET FOREIGN_KEY_CHECKS = 0;
|
||
|
||
-- ----------------------------
|
||
-- 用户表
|
||
-- ----------------------------
|
||
CREATE TABLE `users` (
|
||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
||
`email` VARCHAR(100) NOT NULL COMMENT '用户邮箱',
|
||
`password_hash` VARCHAR(255) NOT NULL COMMENT '密码哈希',
|
||
`nickname` VARCHAR(50) NOT NULL COMMENT '用户昵称',
|
||
`avatar_url` VARCHAR(500) DEFAULT NULL COMMENT '头像URL',
|
||
`status` TINYINT DEFAULT 1 COMMENT '用户状态:1-正常,2-锁定,3-删除',
|
||
`last_login_time` BIGINT DEFAULT NULL COMMENT '最后登录时间戳',
|
||
`created_at` BIGINT NOT NULL COMMENT '创建时间戳',
|
||
`updated_at` BIGINT NOT NULL COMMENT '更新时间戳',
|
||
`deleted_at` BIGINT DEFAULT NULL COMMENT '删除时间戳(软删除)',
|
||
PRIMARY KEY (`id`),
|
||
UNIQUE KEY `uk_email` (`email`),
|
||
KEY `idx_status` (`status`),
|
||
KEY `idx_created_at` (`created_at`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';
|
||
|
||
-- ----------------------------
|
||
-- 会话表
|
||
-- ----------------------------
|
||
CREATE TABLE `sessions` (
|
||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '会话自增ID',
|
||
`session_id` VARCHAR(36) NOT NULL COMMENT '会话UUID',
|
||
`user_id` BIGINT NOT NULL COMMENT '用户ID',
|
||
`title` VARCHAR(200) NOT NULL COMMENT '会话标题',
|
||
`message_count` INT DEFAULT 0 COMMENT '消息数量',
|
||
`last_message_time` BIGINT DEFAULT NULL COMMENT '最后消息时间戳',
|
||
`created_at` BIGINT NOT NULL COMMENT '创建时间戳',
|
||
`updated_at` BIGINT NOT NULL COMMENT '更新时间戳',
|
||
`deleted_at` BIGINT DEFAULT NULL COMMENT '删除时间戳(软删除)',
|
||
PRIMARY KEY (`id`),
|
||
UNIQUE KEY `uk_session_id` (`session_id`),
|
||
KEY `idx_user_id` (`user_id`),
|
||
KEY `idx_last_message_time` (`last_message_time`),
|
||
KEY `idx_created_at` (`created_at`),
|
||
CONSTRAINT `fk_sessions_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='聊天会话表';
|
||
|
||
-- ----------------------------
|
||
-- 消息表
|
||
-- ----------------------------
|
||
CREATE TABLE `messages` (
|
||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '消息自增ID',
|
||
`message_id` VARCHAR(36) NOT NULL COMMENT '消息UUID',
|
||
`session_id` VARCHAR(36) NOT NULL COMMENT '会话ID',
|
||
`user_id` BIGINT NOT NULL COMMENT '用户ID',
|
||
`role` VARCHAR(20) NOT NULL COMMENT '角色:user/assistant',
|
||
`content` LONGTEXT NOT NULL COMMENT '消息内容',
|
||
`model_used` VARCHAR(50) DEFAULT NULL COMMENT '使用的AI模型',
|
||
`deep_thinking` BOOLEAN DEFAULT FALSE COMMENT '是否启用深度思考',
|
||
`web_search` BOOLEAN DEFAULT FALSE COMMENT '是否启用联网搜索',
|
||
`is_favorited` BOOLEAN DEFAULT FALSE COMMENT '是否已收藏',
|
||
`timestamp` BIGINT NOT NULL COMMENT '消息时间戳',
|
||
`created_at` BIGINT NOT NULL COMMENT '创建时间戳',
|
||
`updated_at` BIGINT NOT NULL COMMENT '更新时间戳',
|
||
`deleted_at` BIGINT DEFAULT NULL COMMENT '删除时间戳(软删除)',
|
||
PRIMARY KEY (`id`),
|
||
UNIQUE KEY `uk_message_id` (`message_id`),
|
||
KEY `idx_session_id` (`session_id`),
|
||
KEY `idx_user_id` (`user_id`),
|
||
KEY `idx_role` (`role`),
|
||
KEY `idx_timestamp` (`timestamp`),
|
||
KEY `idx_is_favorited` (`is_favorited`),
|
||
KEY `idx_created_at` (`created_at`),
|
||
CONSTRAINT `fk_messages_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消息表';
|
||
|
||
-- ----------------------------
|
||
-- 收藏表
|
||
-- ----------------------------
|
||
CREATE TABLE `favorites` (
|
||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '收藏自增ID',
|
||
`favorite_id` VARCHAR(36) NOT NULL COMMENT '收藏UUID',
|
||
`user_id` BIGINT NOT NULL COMMENT '用户ID',
|
||
`message_id` VARCHAR(36) NOT NULL COMMENT '消息ID',
|
||
`session_id` VARCHAR(36) NOT NULL COMMENT '会话ID',
|
||
`created_at` BIGINT NOT NULL COMMENT '收藏时间戳',
|
||
`updated_at` BIGINT NOT NULL COMMENT '更新时间戳',
|
||
`deleted_at` BIGINT DEFAULT NULL COMMENT '删除时间戳(软删除)',
|
||
PRIMARY KEY (`id`),
|
||
UNIQUE KEY `uk_favorite_id` (`favorite_id`),
|
||
UNIQUE KEY `uk_user_message` (`user_id`, `message_id`),
|
||
KEY `idx_user_id` (`user_id`),
|
||
KEY `idx_message_id` (`message_id`),
|
||
KEY `idx_session_id` (`session_id`),
|
||
KEY `idx_created_at` (`created_at`),
|
||
CONSTRAINT `fk_favorites_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收藏表';
|
||
|
||
-- ----------------------------
|
||
-- 文件表
|
||
-- ----------------------------
|
||
CREATE TABLE `files` (
|
||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '文件自增ID',
|
||
`file_id` VARCHAR(36) NOT NULL COMMENT '文件UUID',
|
||
`user_id` BIGINT NOT NULL COMMENT '用户ID',
|
||
`session_id` VARCHAR(36) DEFAULT NULL COMMENT '关联会话ID',
|
||
`filename` VARCHAR(255) NOT NULL COMMENT '原始文件名',
|
||
`stored_filename` VARCHAR(255) NOT NULL COMMENT '存储文件名',
|
||
`file_path` VARCHAR(500) NOT NULL COMMENT '文件存储路径',
|
||
`file_size` BIGINT NOT NULL COMMENT '文件大小(字节)',
|
||
`file_type` VARCHAR(100) NOT NULL COMMENT '文件类型',
|
||
`mime_type` VARCHAR(100) DEFAULT NULL COMMENT 'MIME类型',
|
||
`upload_time` BIGINT NOT NULL COMMENT '上传时间戳',
|
||
`created_at` BIGINT NOT NULL COMMENT '创建时间戳',
|
||
`updated_at` BIGINT NOT NULL COMMENT '更新时间戳',
|
||
`deleted_at` BIGINT DEFAULT NULL COMMENT '删除时间戳(软删除)',
|
||
PRIMARY KEY (`id`),
|
||
UNIQUE KEY `uk_file_id` (`file_id`),
|
||
KEY `idx_user_id` (`user_id`),
|
||
KEY `idx_session_id` (`session_id`),
|
||
KEY `idx_file_type` (`file_type`),
|
||
KEY `idx_upload_time` (`upload_time`),
|
||
KEY `idx_created_at` (`created_at`),
|
||
CONSTRAINT `fk_files_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文件表';
|
||
|
||
-- ----------------------------
|
||
-- 操作日志表
|
||
-- ----------------------------
|
||
CREATE TABLE `operation_logs` (
|
||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '日志ID',
|
||
`user_id` BIGINT DEFAULT NULL COMMENT '操作用户ID',
|
||
`operation_type` VARCHAR(50) NOT NULL COMMENT '操作类型',
|
||
`operation_desc` VARCHAR(500) DEFAULT NULL COMMENT '操作描述',
|
||
`ip_address` VARCHAR(50) DEFAULT NULL COMMENT 'IP地址',
|
||
`user_agent` VARCHAR(500) DEFAULT NULL COMMENT '用户代理',
|
||
`request_params` TEXT DEFAULT NULL COMMENT '请求参数',
|
||
`response_result` TEXT DEFAULT NULL COMMENT '响应结果',
|
||
`execution_time` INT DEFAULT NULL COMMENT '执行时长(毫秒)',
|
||
`created_at` BIGINT NOT NULL COMMENT '创建时间戳',
|
||
PRIMARY KEY (`id`),
|
||
KEY `idx_user_id` (`user_id`),
|
||
KEY `idx_operation_type` (`operation_type`),
|
||
KEY `idx_created_at` (`created_at`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='操作日志表';
|
||
|
||
-- ----------------------------
|
||
-- 系统配置表
|
||
-- ----------------------------
|
||
CREATE TABLE `system_configs` (
|
||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '配置ID',
|
||
`config_key` VARCHAR(100) NOT NULL COMMENT '配置键',
|
||
`config_value` TEXT DEFAULT NULL COMMENT '配置值',
|
||
`config_desc` VARCHAR(500) DEFAULT NULL COMMENT '配置描述',
|
||
`config_type` VARCHAR(20) DEFAULT 'STRING' COMMENT '配置类型:STRING/NUMBER/BOOLEAN/JSON',
|
||
`is_encrypted` BOOLEAN DEFAULT FALSE COMMENT '是否加密存储',
|
||
`created_at` BIGINT NOT NULL COMMENT '创建时间戳',
|
||
`updated_at` BIGINT NOT NULL COMMENT '更新时间戳',
|
||
PRIMARY KEY (`id`),
|
||
UNIQUE KEY `uk_config_key` (`config_key`),
|
||
KEY `idx_config_type` (`config_type`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统配置表';
|
||
|
||
SET FOREIGN_KEY_CHECKS = 1; |