夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 1 | -- 用户表(保持不变) |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 2 | CREATE TABLE IF NOT EXISTS `users` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 3 | `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 |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 8 | ); |
wuchimedes | a26ed3f | 2025-04-25 14:41:09 +0800 | [diff] [blame^] | 9 | -- 用户统计表 |
wuchimedes | 22ee83c | 2025-04-25 00:17:47 +0800 | [diff] [blame] | 10 | CREATE 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 | ); |
wuchimedes | a26ed3f | 2025-04-25 14:41:09 +0800 | [diff] [blame^] | 19 | -- 种子表 |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 20 | CREATE TABLE IF NOT EXISTS `torrents` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 21 | `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`) |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 27 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 28 | -- Peer表(保持不变) |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 29 | CREATE TABLE IF NOT EXISTS `peers` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 30 | `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`) |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 40 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 41 | -- 帖子表(新增 hot_score 和 last_calculated 字段) |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 42 | CREATE TABLE IF NOT EXISTS `posts` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 43 | `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`) -- 新增类型索引 |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 55 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 56 | -- 标签表(保持不变) |
wuchimedes | 03f9358 | 2025-04-21 15:32:59 +0800 | [diff] [blame] | 57 | CREATE TABLE IF NOT EXISTS `tags`( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 58 | `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`) |
wuchimedes | 03f9358 | 2025-04-21 15:32:59 +0800 | [diff] [blame] | 62 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 63 | -- 帖子标签关联表(保持不变) |
wuchimedes | 03f9358 | 2025-04-21 15:32:59 +0800 | [diff] [blame] | 64 | CREATE TABLE IF NOT EXISTS `post_tag` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 65 | `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`) |
wuchimedes | 03f9358 | 2025-04-21 15:32:59 +0800 | [diff] [blame] | 70 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 71 | -- 用户关注表(保持不变) |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 72 | CREATE TABLE IF NOT EXISTS `user_follows` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 73 | `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`) |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 79 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 80 | -- 私信表(保持不变) |
夜雨声烦 | 7e6eb38 | 2025-04-22 01:18:00 +0800 | [diff] [blame] | 81 | CREATE TABLE IF NOT EXISTS `private_messages` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 82 | `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`) |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 90 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 91 | -- 评论表(保持不变) |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 92 | CREATE TABLE IF NOT EXISTS `comments` ( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 93 | `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`) -- 新增评论帖子索引 |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 103 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 104 | -- 帖子点赞表(保持不变) |
| 105 | CREATE 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`) |
夜雨声烦 | 0206359 | 2025-04-23 18:10:00 +0800 | [diff] [blame] | 112 | ); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 113 | -- 帖子浏览记录表(新增复合索引) |
| 114 | CREATE 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) -- 新增用户浏览时间索引 |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 122 | ); |
| 123 | CREATE 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) |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 131 | ); |