blob: e5ddbb42093a281668eebb86a0a23088b84c458c [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);
9
夜雨声烦368e3562025-04-24 01:49:46 +080010-- 种子表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080011CREATE TABLE IF NOT EXISTS `torrents` (
夜雨声烦368e3562025-04-24 01:49:46 +080012 `torrent_id` INT AUTO_INCREMENT PRIMARY KEY,
13 `user_id` INT NOT NULL,
14 `torrent_name` VARCHAR(255) NOT NULL,
15 `info_hash` BINARY(20) NOT NULL,
16 `file_size` FLOAT NOT NULL,
17 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
wuchimedes079c1632025-04-02 22:01:20 +080018);
19
夜雨声烦368e3562025-04-24 01:49:46 +080020-- Peer表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080021CREATE TABLE IF NOT EXISTS `peers` (
夜雨声烦368e3562025-04-24 01:49:46 +080022 `passkey` VARCHAR(255) NOT NULL,
23 `info_hash` BINARY(20) NOT NULL,
24 `peer_id` VARCHAR(20) NOT NULL,
25 `ip_address` VARCHAR(128) NOT NULL,
26 `port` INT NOT NULL,
27 `uploaded` FLOAT NOT NULL,
28 `downloaded` FLOAT NOT NULL,
29 `last_seen` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
30 FOREIGN KEY (`passkey`) REFERENCES `users`(`passkey`),
31 PRIMARY KEY (`passkey`, `info_hash`, `peer_id`)
wuchimedes079c1632025-04-02 22:01:20 +080032);
wuchimedese5722e32025-04-13 17:38:50 +080033
夜雨声烦368e3562025-04-24 01:49:46 +080034-- 帖子表(新增 hot_score 和 last_calculated 字段)
wuchimedese5722e32025-04-13 17:38:50 +080035CREATE TABLE IF NOT EXISTS `posts` (
夜雨声烦368e3562025-04-24 01:49:46 +080036 `post_id` INT AUTO_INCREMENT PRIMARY KEY,
37 `user_id` INT NOT NULL,
38 `hot_score` DOUBLE DEFAULT 5.0 COMMENT '热度评分',
39 `view_count` INT DEFAULT 0 COMMENT '浏览数',
40 `post_title` VARCHAR(255) NOT NULL,
41 `post_content` TEXT NOT NULL,
42 `post_type` ENUM('resource', 'discussion') NOT NULL,
43 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
44 `last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
45 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
46 INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
47 INDEX `idx_post_type` (`post_type`) -- 新增类型索引
wuchimedese5722e32025-04-13 17:38:50 +080048);
49
夜雨声烦368e3562025-04-24 01:49:46 +080050-- 标签表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080051CREATE TABLE IF NOT EXISTS `tags`(
夜雨声烦368e3562025-04-24 01:49:46 +080052 `tag_id` INT AUTO_INCREMENT PRIMARY KEY,
53 `tag_name` VARCHAR(255) NOT NULL UNIQUE,
54 `parent_id` INT DEFAULT NULL,
55 FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080056);
57
夜雨声烦368e3562025-04-24 01:49:46 +080058-- 帖子标签关联表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080059CREATE TABLE IF NOT EXISTS `post_tag` (
夜雨声烦368e3562025-04-24 01:49:46 +080060 `post_id` INT NOT NULL,
61 `tag_id` INT NOT NULL,
62 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
63 FOREIGN KEY (`tag_id`) REFERENCES `tags`(`tag_id`),
64 PRIMARY KEY (`post_id`, `tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080065);
66
夜雨声烦368e3562025-04-24 01:49:46 +080067-- 用户关注表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080068CREATE TABLE IF NOT EXISTS `user_follows` (
夜雨声烦368e3562025-04-24 01:49:46 +080069 `follower_id` INT NOT NULL,
70 `followed_id` INT NOT NULL,
71 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
72 FOREIGN KEY (`follower_id`) REFERENCES `users`(`user_id`),
73 FOREIGN KEY (`followed_id`) REFERENCES `users`(`user_id`),
74 PRIMARY KEY (`follower_id`, `followed_id`)
夜雨声烦7e6eb382025-04-22 01:18:00 +080075);
76
夜雨声烦368e3562025-04-24 01:49:46 +080077-- 私信表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080078CREATE TABLE IF NOT EXISTS `private_messages` (
夜雨声烦368e3562025-04-24 01:49:46 +080079 `message_id` INT AUTO_INCREMENT PRIMARY KEY,
80 `sender_id` INT NOT NULL,
81 `receiver_id` INT NOT NULL,
82 `content` TEXT NOT NULL,
83 `sent_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
84 `is_read` BOOLEAN DEFAULT false,
85 FOREIGN KEY (`sender_id`) REFERENCES `users`(`user_id`),
86 FOREIGN KEY (`receiver_id`) REFERENCES `users`(`user_id`)
夜雨声烦4527a722025-04-23 17:04:25 +080087);
88
夜雨声烦368e3562025-04-24 01:49:46 +080089-- 评论表(保持不变)
夜雨声烦4527a722025-04-23 17:04:25 +080090CREATE TABLE IF NOT EXISTS `comments` (
夜雨声烦368e3562025-04-24 01:49:46 +080091 `comment_id` INT AUTO_INCREMENT PRIMARY KEY,
92 `post_id` INT NOT NULL,
93 `user_id` INT NOT NULL,
94 `parent_comment_id` INT DEFAULT NULL,
95 `content` TEXT NOT NULL,
96 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
97 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
98 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
99 FOREIGN KEY (`parent_comment_id`) REFERENCES `comments`(`comment_id`),
100 INDEX `idx_post_id` (`post_id`) -- 新增评论帖子索引
夜雨声烦4527a722025-04-23 17:04:25 +0800101);
夜雨声烦02063592025-04-23 18:10:00 +0800102
夜雨声烦368e3562025-04-24 01:49:46 +0800103-- 帖子点赞表(保持不变)
104CREATE TABLE IF NOT EXISTS `post_likes` (
105 `user_id` INT NOT NULL,
106 `post_id` INT NOT NULL,
107 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
108 PRIMARY KEY (`user_id`, `post_id`),
109 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
110 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
夜雨声烦02063592025-04-23 18:10:00 +0800111);
夜雨声烦f4b8b6f2025-04-24 00:58:36 +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) -- 新增用户浏览时间索引
122);