1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# 用户表
CREATE TABLE tb_users (
user_id BIGINT PRIMARY KEY AUTO_INCREMENT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL
);
# 消息表
CREATE TABLE tb_messages (
message_id BIGINT PRIMARY KEY AUTO_INCREMENT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
content TEXT NOT NULL,
state ENUM('sent', 'delivered', 'read') NOT NULL DEFAULT 'sent',
sender_user_id BIGINT NOT NULL,
receiver_user_id BIGINT NOT NULL,
FOREIGN KEY (sender_user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE,
FOREIGN KEY (receiver_user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE
);
# 好友表
CREATE TABLE tb_friends (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT NOT NULL,
friend_id BIGINT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
status ENUM('pending', 'accepted', 'blocked') NOT NULL DEFAULT 'pending',
FOREIGN KEY (user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE,
FOREIGN KEY (friend_id) REFERENCES tb_users(user_id) ON DELETE CASCADE,
UNIQUE KEY unique_friendship (user_id, friend_id)
);
# 黑名单表
CREATE TABLE tb_blacklist (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT NOT NULL,
blocked_user_id BIGINT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE,
FOREIGN KEY (blocked_user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE,
UNIQUE KEY unique_blacklist (user_id, blocked_user_id)
);
# 群聊表
CREATE TABLE tb_groups (
group_id BIGINT PRIMARY KEY AUTO_INCREMENT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
group_name VARCHAR(255) NOT NULL,
owner_user_id BIGINT NOT NULL,
FOREIGN KEY (owner_user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE
);
# 群聊成员表
CREATE TABLE tb_group_members (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
group_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
joined_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
role ENUM('member', 'admin', 'owner') NOT NULL DEFAULT 'member',
FOREIGN KEY (group_id) REFERENCES tb_groups(group_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE,
UNIQUE KEY unique_group_membership (group_id, user_id)
);
# 群聊消息表
CREATE TABLE tb_group_messages (
message_id BIGINT PRIMARY KEY AUTO_INCREMENT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
content TEXT NOT NULL,
sender_user_id BIGINT NOT NULL,
group_id BIGINT NOT NULL,
FOREIGN KEY (sender_user_id) REFERENCES tb_users(user_id) ON DELETE CASCADE,
FOREIGN KEY (group_id) REFERENCES tb_groups(group_id) ON DELETE CASCADE
);
# 群读取消息偏移量表
CREATE TABLE tb_group_device_read_offset (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
group_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
device_id VARCHAR(255) NOT NULL,
last_read_msg_id BIGINT NOT NULL DEFAULT 0,
last_read_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY unique_device_read_offset (group_id, user_id, device_id)
);
# 单聊读取消息偏移量表
CREATE TABLE tb_device_read_offset (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT NOT NULL,
friend_user_id BIGINT NOT NULL,
device_id VARCHAR(255) NOT NULL,
last_read_msg_id BIGINT NOT NULL DEFAULT 0,
last_read_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY unique_device_read_offset (friend_user_id, user_id, device_id)
);
|