llm-chat/backend/database/schema.sql

30 lines
1.2 KiB
MySQL
Raw Normal View History

-- 创建对话表
CREATE TABLE IF NOT EXISTS conversations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL DEFAULT '新对话',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- 创建消息表
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
conversation_id INTEGER NOT NULL,
role TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
content TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE
);
-- 创建索引以提高查询性能
CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON messages(conversation_id);
CREATE INDEX IF NOT EXISTS idx_messages_timestamp ON messages(timestamp);
CREATE INDEX IF NOT EXISTS idx_conversations_updated_at ON conversations(updated_at);
-- 插入示例数据
INSERT OR IGNORE INTO conversations (id, title, created_at, updated_at)
VALUES (1, '欢迎对话', datetime('now'), datetime('now'));
INSERT OR IGNORE INTO messages (conversation_id, role, content, timestamp)
VALUES
(1, 'assistant', '你好我是AI助手很高兴为您服务。有什么我可以帮助您的吗', datetime('now'));