blob: c0659b30e7fe0d4e86016c37dba5f0542e29161d [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);
夜雨声烦368e3562025-04-24 01:49:46 +08009-- 种子表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080010CREATE TABLE IF NOT EXISTS `torrents` (
夜雨声烦368e3562025-04-24 01:49:46 +080011 `torrent_id` INT AUTO_INCREMENT PRIMARY KEY,
12 `user_id` INT NOT NULL,
13 `torrent_name` VARCHAR(255) NOT NULL,
14 `info_hash` BINARY(20) NOT NULL,
15 `file_size` FLOAT NOT NULL,
16 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
wuchimedes079c1632025-04-02 22:01:20 +080017);
夜雨声烦368e3562025-04-24 01:49:46 +080018-- Peer表(保持不变)
wuchimedes079c1632025-04-02 22:01:20 +080019CREATE TABLE IF NOT EXISTS `peers` (
夜雨声烦368e3562025-04-24 01:49:46 +080020 `passkey` VARCHAR(255) NOT NULL,
21 `info_hash` BINARY(20) NOT NULL,
22 `peer_id` VARCHAR(20) NOT NULL,
23 `ip_address` VARCHAR(128) NOT NULL,
24 `port` INT NOT NULL,
25 `uploaded` FLOAT NOT NULL,
26 `downloaded` FLOAT NOT NULL,
27 `last_seen` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28 FOREIGN KEY (`passkey`) REFERENCES `users`(`passkey`),
29 PRIMARY KEY (`passkey`, `info_hash`, `peer_id`)
wuchimedes079c1632025-04-02 22:01:20 +080030);
夜雨声烦368e3562025-04-24 01:49:46 +080031-- 帖子表(新增 hot_score 和 last_calculated 字段)
wuchimedese5722e32025-04-13 17:38:50 +080032CREATE TABLE IF NOT EXISTS `posts` (
夜雨声烦368e3562025-04-24 01:49:46 +080033 `post_id` INT AUTO_INCREMENT PRIMARY KEY,
34 `user_id` INT NOT NULL,
35 `hot_score` DOUBLE DEFAULT 5.0 COMMENT '热度评分',
36 `view_count` INT DEFAULT 0 COMMENT '浏览数',
37 `post_title` VARCHAR(255) NOT NULL,
38 `post_content` TEXT NOT NULL,
39 `post_type` ENUM('resource', 'discussion') NOT NULL,
40 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
41 `last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
42 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
43 INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
44 INDEX `idx_post_type` (`post_type`) -- 新增类型索引
wuchimedese5722e32025-04-13 17:38:50 +080045);
夜雨声烦368e3562025-04-24 01:49:46 +080046-- 标签表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080047CREATE TABLE IF NOT EXISTS `tags`(
夜雨声烦368e3562025-04-24 01:49:46 +080048 `tag_id` INT AUTO_INCREMENT PRIMARY KEY,
49 `tag_name` VARCHAR(255) NOT NULL UNIQUE,
50 `parent_id` INT DEFAULT NULL,
51 FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080052);
夜雨声烦368e3562025-04-24 01:49:46 +080053-- 帖子标签关联表(保持不变)
wuchimedes03f93582025-04-21 15:32:59 +080054CREATE TABLE IF NOT EXISTS `post_tag` (
夜雨声烦368e3562025-04-24 01:49:46 +080055 `post_id` INT NOT NULL,
56 `tag_id` INT NOT NULL,
57 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
58 FOREIGN KEY (`tag_id`) REFERENCES `tags`(`tag_id`),
59 PRIMARY KEY (`post_id`, `tag_id`)
wuchimedes03f93582025-04-21 15:32:59 +080060);
夜雨声烦368e3562025-04-24 01:49:46 +080061-- 用户关注表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080062CREATE TABLE IF NOT EXISTS `user_follows` (
夜雨声烦368e3562025-04-24 01:49:46 +080063 `follower_id` INT NOT NULL,
64 `followed_id` INT NOT NULL,
65 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
66 FOREIGN KEY (`follower_id`) REFERENCES `users`(`user_id`),
67 FOREIGN KEY (`followed_id`) REFERENCES `users`(`user_id`),
68 PRIMARY KEY (`follower_id`, `followed_id`)
夜雨声烦7e6eb382025-04-22 01:18:00 +080069);
夜雨声烦368e3562025-04-24 01:49:46 +080070-- 私信表(保持不变)
夜雨声烦7e6eb382025-04-22 01:18:00 +080071CREATE TABLE IF NOT EXISTS `private_messages` (
夜雨声烦368e3562025-04-24 01:49:46 +080072 `message_id` INT AUTO_INCREMENT PRIMARY KEY,
73 `sender_id` INT NOT NULL,
74 `receiver_id` INT NOT NULL,
75 `content` TEXT NOT NULL,
76 `sent_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
77 `is_read` BOOLEAN DEFAULT false,
78 FOREIGN KEY (`sender_id`) REFERENCES `users`(`user_id`),
79 FOREIGN KEY (`receiver_id`) REFERENCES `users`(`user_id`)
夜雨声烦4527a722025-04-23 17:04:25 +080080);
夜雨声烦368e3562025-04-24 01:49:46 +080081-- 评论表(保持不变)
夜雨声烦4527a722025-04-23 17:04:25 +080082CREATE TABLE IF NOT EXISTS `comments` (
夜雨声烦368e3562025-04-24 01:49:46 +080083 `comment_id` INT AUTO_INCREMENT PRIMARY KEY,
84 `post_id` INT NOT NULL,
85 `user_id` INT NOT NULL,
86 `parent_comment_id` INT DEFAULT NULL,
87 `content` TEXT NOT NULL,
88 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
89 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
90 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
91 FOREIGN KEY (`parent_comment_id`) REFERENCES `comments`(`comment_id`),
92 INDEX `idx_post_id` (`post_id`) -- 新增评论帖子索引
夜雨声烦4527a722025-04-23 17:04:25 +080093);
夜雨声烦368e3562025-04-24 01:49:46 +080094-- 帖子点赞表(保持不变)
95CREATE TABLE IF NOT EXISTS `post_likes` (
96 `user_id` INT NOT NULL,
97 `post_id` INT NOT NULL,
98 `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
99 PRIMARY KEY (`user_id`, `post_id`),
100 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
101 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
夜雨声烦02063592025-04-23 18:10:00 +0800102);
夜雨声烦368e3562025-04-24 01:49:46 +0800103-- 帖子浏览记录表(新增复合索引)
104CREATE TABLE IF NOT EXISTS `post_views` (
105 `view_id` INT AUTO_INCREMENT PRIMARY KEY,
106 `user_id` INT NOT NULL,
107 `post_id` INT NOT NULL,
108 `view_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
109 FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
110 FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
111 INDEX `idx_user_view_time` (`user_id`, `view_time` DESC) -- 新增用户浏览时间索引
夜雨声烦f77d8132025-04-24 19:31:18 +0800112);
113CREATE TABLE user_tag_preference (
114 user_id INT NOT NULL COMMENT '用户ID',
115 tag_id INT NOT NULL COMMENT '标签ID',
116 weight DOUBLE DEFAULT 1.0 COMMENT '偏好权重(浏览越多权重越高)',
117 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
118 PRIMARY KEY (user_id, tag_id),
119 FOREIGN KEY (user_id) REFERENCES users(user_id),
120 FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
夜雨声烦368e3562025-04-24 01:49:46 +0800121);