blob: 5cb15a51625172d1d62cf6a2fd5e8a9f351fb7ce [file] [log] [blame]
夜雨声烦368e3562025-04-24 01:49:46 +08001-- 用户表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +08002CREATE TABLE IF NOT EXISTS `users` (
夜雨声烦368e3562025-04-24 01:49:46 +08003 `user_id` INT AUTO_INCREMENT PRIMARY KEY,
4 `user_name` VARCHAR(255) NOT NULL,
5 `password` VARCHAR(255) NOT NULL,
6 `email` VARCHAR(255) NOT NULL UNIQUE,
7 `passkey` VARCHAR(255) NOT NULL UNIQUE
wuchimedes079c1632025-04-02 22:01:20 +08008);
wuchimedesa26ed3f2025-04-25 14:41:09 +08009-- 用户统计表
wuchimedes22ee83c2025-04-25 00:17:47 +080010CREATE TABLE IF NOT EXISTS `user_stats` (
11 user_id INT PRIMARY KEY,
12 passkey VARCHAR(255) NOT NULL UNIQUE,
13 total_upload FLOAT NOT NULL DEFAULT 0,
14 total_download FLOAT NOT NULL DEFAULT 0,
15 last_update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
16 FOREIGN KEY (user_id) REFERENCES users(user_id),
17 FOREIGN KEY (passkey) REFERENCES users(passkey)
18);
wuchimedesa26ed3f2025-04-25 14:41:09 +080019-- 种子表
wuchimedes079c1632025-04-02 22:01:20 +080020CREATE TABLE IF NOT EXISTS `torrents` (
夜雨声烦368e3562025-04-24 01:49:46 +080021 `torrent_id` INT AUTO_INCREMENT PRIMARY KEY,
22 `user_id` INT NOT NULL,
23 `torrent_name` VARCHAR(255) NOT NULL,
24 `info_hash` BINARY(20) NOT NULL,
25 `file_size` FLOAT NOT NULL,
26 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
wuchimedes079c1632025-04-02 22:01:20 +080027);
夜雨声烦368e3562025-04-24 01:49:46 +080028-- Peer表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080029CREATE TABLE IF NOT EXISTS `peers` (
夜雨声烦368e3562025-04-24 01:49:46 +080030 `passkey` VARCHAR(255) NOT NULL,
31 `info_hash` BINARY(20) NOT NULL,
32 `peer_id` VARCHAR(20) NOT NULL,
33 `ip_address` VARCHAR(128) NOT NULL,
34 `port` INT NOT NULL,
35 `uploaded` FLOAT NOT NULL,
36 `downloaded` FLOAT NOT NULL,
37 `last_seen` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
38 FOREIGN KEY (`passkey`) REFERENCES `users`(`passkey`),
39 PRIMARY KEY (`passkey`, `info_hash`, `peer_id`)
wuchimedes079c1632025-04-02 22:01:20 +080040);
夜雨声烦368e3562025-04-24 01:49:46 +080041-- 帖子表(新增 hot_score 和 last_calculated 字段)
wuchimedese5722e32025-04-13 17:38:50 +080042CREATE TABLE IF NOT EXISTS `posts` (
夜雨声烦368e3562025-04-24 01:49:46 +080043 `post_id` INT AUTO_INCREMENT PRIMARY KEY,
44 `user_id` INT NOT NULL,
45 `hot_score` DOUBLE DEFAULT 5.0 COMMENT '热度评分',
46 `view_count` INT DEFAULT 0 COMMENT '浏览数',
47 `post_title` VARCHAR(255) NOT NULL,
48 `post_content` TEXT NOT NULL,
49 `post_type` ENUM('resource', 'discussion') NOT NULL,
50 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
51 `last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
52 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
53 INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
54 INDEX `idx_post_type` (`post_type`) -- 新增类型索引
wuchimedese5722e32025-04-13 17:38:50 +080055);
夜雨声烦368e3562025-04-24 01:49:46 +080056-- 标签表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080057CREATE TABLE IF NOT EXISTS `tags`(
夜雨声烦368e3562025-04-24 01:49:46 +080058 `tag_id` INT AUTO_INCREMENT PRIMARY KEY,
59 `tag_name` VARCHAR(255) NOT NULL UNIQUE,
60 `parent_id` INT DEFAULT NULL,
61 FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080062);
夜雨声烦368e3562025-04-24 01:49:46 +080063-- 帖子标签关联表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080064CREATE TABLE IF NOT EXISTS `post_tag` (
夜雨声烦368e3562025-04-24 01:49:46 +080065 `post_id` INT NOT NULL,
66 `tag_id` INT NOT NULL,
67 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
68 FOREIGN KEY (`tag_id`) REFERENCES `tags`(`tag_id`),
69 PRIMARY KEY (`post_id`, `tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080070);
夜雨声烦368e3562025-04-24 01:49:46 +080071-- 用户关注表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080072CREATE TABLE IF NOT EXISTS `user_follows` (
夜雨声烦368e3562025-04-24 01:49:46 +080073 `follower_id` INT NOT NULL,
74 `followed_id` INT NOT NULL,
75 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
76 FOREIGN KEY (`follower_id`) REFERENCES `users`(`user_id`),
77 FOREIGN KEY (`followed_id`) REFERENCES `users`(`user_id`),
78 PRIMARY KEY (`follower_id`, `followed_id`)
夜雨声烦7e6eb382025-04-22 01:18:00 +080079);
夜雨声烦368e3562025-04-24 01:49:46 +080080-- 私信表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080081CREATE TABLE IF NOT EXISTS `private_messages` (
夜雨声烦368e3562025-04-24 01:49:46 +080082 `message_id` INT AUTO_INCREMENT PRIMARY KEY,
83 `sender_id` INT NOT NULL,
84 `receiver_id` INT NOT NULL,
85 `content` TEXT NOT NULL,
86 `sent_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
87 `is_read` BOOLEAN DEFAULT false,
88 FOREIGN KEY (`sender_id`) REFERENCES `users`(`user_id`),
89 FOREIGN KEY (`receiver_id`) REFERENCES `users`(`user_id`)
夜雨声烦4527a722025-04-23 17:04:25 +080090);
夜雨声烦368e3562025-04-24 01:49:46 +080091-- 评论表(保持不变)
夜雨声烦4527a722025-04-23 17:04:25 +080092CREATE TABLE IF NOT EXISTS `comments` (
夜雨声烦368e3562025-04-24 01:49:46 +080093 `comment_id` INT AUTO_INCREMENT PRIMARY KEY,
94 `post_id` INT NOT NULL,
95 `user_id` INT NOT NULL,
96 `parent_comment_id` INT DEFAULT NULL,
97 `content` TEXT NOT NULL,
98 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
99 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
100 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
101 FOREIGN KEY (`parent_comment_id`) REFERENCES `comments`(`comment_id`),
102 INDEX `idx_post_id` (`post_id`) -- 新增评论帖子索引
夜雨声烦4527a722025-04-23 17:04:25 +0800103);
夜雨声烦368e3562025-04-24 01:49:46 +0800104-- 帖子点赞表(保持不变)
105CREATE TABLE IF NOT EXISTS `post_likes` (
106 `user_id` INT NOT NULL,
107 `post_id` INT NOT NULL,
108 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
109 PRIMARY KEY (`user_id`, `post_id`),
110 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
111 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
夜雨声烦02063592025-04-23 18:10:00 +0800112);
夜雨声烦368e3562025-04-24 01:49:46 +0800113-- 帖子浏览记录表(新增复合索引)
114CREATE TABLE IF NOT EXISTS `post_views` (
115 `view_id` INT AUTO_INCREMENT PRIMARY KEY,
116 `user_id` INT NOT NULL,
117 `post_id` INT NOT NULL,
118 `view_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
119 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
120 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
121 INDEX `idx_user_view_time` (`user_id`, `view_time` DESC) -- 新增用户浏览时间索引
夜雨声烦f77d8132025-04-24 19:31:18 +0800122);
123CREATE TABLE user_tag_preference (
124 user_id INT NOT NULL COMMENT '用户ID',
125 tag_id INT NOT NULL COMMENT '标签ID',
126 weight DOUBLE DEFAULT 1.0 COMMENT '偏好权重(浏览越多权重越高)',
127 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
128 PRIMARY KEY (user_id, tag_id),
129 FOREIGN KEY (user_id) REFERENCES users(user_id),
130 FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
223010718e412ad2025-04-24 22:24:51 +0800131);